0


C# 创建WebService接口并连接

创建WebService项目

首先安装下.NET Framework4.6.2-4.7.1开发工具。
在这里插入图片描述
然后就是新建

ASP.NET Web应用程序

项目。
在这里插入图片描述
输入项目名称

WebServiceDemo

在这里插入图片描述选择空,然后先去掉HTTPS配置。
在这里插入图片描述
项目创建好之后,开始添加

asmx

文件.
在这里插入图片描述
添加好之后在添加一个有参数的名为

Hello

的方法。代码如下图。

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Services;namespaceWebServiceDemo{/// <summary>/// WebService1 的摘要说明/// </summary>[WebService(Namespace ="http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService]publicclassWebService1:System.Web.Services.WebService{[WebMethod]publicstringHelloWorld(){return"Hello World";}[WebMethod]publicstringHello(string name){return"Hello"+name;}}}

然后就可以直接启动了,也可以发布到IIS中启动。这里先发布到IIS,一会在新建一个控制台项目用于连接到该服务。
发布好之后在IIS中添加网站,并绑定端口号为81.然后就可以启动了。
直接启动的话可能会报下面的错误,这是因为没有设置起始页。
在这里插入图片描述
可以直接输入地址访问。

http://localhost:81/webservice1.asmx

也可以在IIS默认文档中添加

webservice1.asmx

文件。下次在浏览就可以直接打开了。
在这里插入图片描述
出现下图的页面,就表示服务已经部署成功了。
在这里插入图片描述

连接到WebService服务

新建一个控制台应用。
然后打开webservice地址输入。

http://localhost:81/webservice1.asmx?wsdl

会打开一个xml文件。
在这里插入图片描述
接着右键文件另存为,把文件保存下来。并修改文件后缀名为

wsdl


在这里插入图片描述
在VS中添加,添加服务引用。选择WCF Web Service。
在这里插入图片描述
这里其实可以直接输入WebService的地址点击转到即可。当考虑到要连接的服务在本地不一定是可以访问的,所以我们可以点击浏览通过上面生成的wsdl文件来生成对应的代码。
在这里插入图片描述
添加进来后如下图所示,命名空间可以按照实际名称修改。
在这里插入图片描述
之后点击下一步,然后点击完成即可。
在这里插入图片描述
完成之后这里就多了两个文件。
在这里插入图片描述
调用方式如下,直接实例化对应的类,然后就可以像调用普通方法一样,调用远程的服务接口了。

usingServiceReference1;usingSystem;usingSystem.Threading.Tasks;namespaceTestProject{publicclassProgram{staticasyncTaskMain(string[] args){awaitTest();}publicstaticasyncTaskTest(){var reference =newWebService1SoapClient(WebService1SoapClient.EndpointConfiguration.WebService1Soap12);var helloWorldResult =await reference.HelloWorldAsync();
            Console.WriteLine(helloWorldResult.Body.HelloWorldResult);var str ="张三";var helloResult =await reference.HelloAsync(str);
            Console.WriteLine(helloResult.Body.HelloResult);}}}

返回结果如下,就像调用本地方法一样自然。
在这里插入图片描述

不过这里应该有地方需要按需修改一下,在

Reference.cs

文件中,远程服务地址是写死的。所以需要改成参数。

privatestaticSystem.ServiceModel.EndpointAddressGetEndpointAddress(EndpointConfiguration endpointConfiguration){if((endpointConfiguration == EndpointConfiguration.WebService1Soap)){returnnewSystem.ServiceModel.EndpointAddress("http://localhost:81/webservice1.asmx");}if((endpointConfiguration == EndpointConfiguration.WebService1Soap12)){returnnewSystem.ServiceModel.EndpointAddress("http://localhost:81/webservice1.asmx");}thrownewSystem.InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration));}

改造方法也简单。添加一个url的入参。

privatestaticSystem.ServiceModel.EndpointAddressGetEndpointAddress(string url,EndpointConfiguration endpointConfiguration){if((endpointConfiguration == EndpointConfiguration.WebService1Soap)){returnnewSystem.ServiceModel.EndpointAddress(url);}if((endpointConfiguration == EndpointConfiguration.WebService1Soap12)){returnnewSystem.ServiceModel.EndpointAddress(url);}thrownewSystem.InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration));}

以及引用这个方法的这里都加上url。

publicWebService1SoapClient(string url,EndpointConfiguration endpointConfiguration):base(WebService1SoapClient.GetBindingForEndpoint(endpointConfiguration), WebService1SoapClient.GetEndpointAddress(url,endpointConfiguration)){this.Endpoint.Name = endpointConfiguration.ToString();ConfigureEndpoint(this.Endpoint,this.ClientCredentials);}

调用的时候把Url传进去即可。

var url ="http://localhost:81/webservice1.asmx";var reference =newWebService1SoapClient(url, WebService1SoapClient.EndpointConfiguration.WebService1Soap12);var helloWorldResult =await reference.HelloWorldAsync();
Console.WriteLine(helloWorldResult.Body.HelloWorldResult);var str ="张三";var helloResult =await reference.HelloAsync(str);
Console.WriteLine(helloResult.Body.HelloResult);

have a wonderful day。

标签: c# asp.net WebService

本文转载自: https://blog.csdn.net/u012869793/article/details/128341811
版权归原作者 李公子lm 所有, 如有侵权,请联系我们删除。

“C# 创建WebService接口并连接”的评论:

还没有评论