C#創(chuàng)建WebService接口并連接的全過(guò)程
創(chuàng)建WebService項(xiàng)目
首先安裝下.NET Framework4.6.2-4.7.1開(kāi)發(fā)工具。
然后就是新建 ASP.NET Web應(yīng)用程序 項(xiàng)目。
輸入項(xiàng)目名稱(chēng)WebServiceDemo
選擇空,然后先去掉HTTPS配置。
項(xiàng)目創(chuàng)建好之后,開(kāi)始添加asmx
文件.
添加好之后在添加一個(gè)有參數(shù)的名為Hello
的方法。代碼如下圖。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebServiceDemo { /// <summary> /// WebService1 的摘要說(shuō)明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消注釋以下行。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string Hello(string name) { return "Hello"+name; } } }
然后就可以直接啟動(dòng)了,也可以發(fā)布到IIS中啟動(dòng)。這里先發(fā)布到IIS,一會(huì)在新建一個(gè)控制臺(tái)項(xiàng)目用于連接到該服務(wù)。
發(fā)布好之后在IIS中添加網(wǎng)站,并綁定端口號(hào)為81.然后就可以啟動(dòng)了。
直接啟動(dòng)的話可能會(huì)報(bào)下面的錯(cuò)誤,這是因?yàn)闆](méi)有設(shè)置起始頁(yè)。
可以直接輸入地址訪問(wèn)。
http://localhost:81/webservice1.asmx
也可以在IIS默認(rèn)文檔中添加webservice1.asmx
文件。下次在瀏覽就可以直接打開(kāi)了。
出現(xiàn)下圖的頁(yè)面,就表示服務(wù)已經(jīng)部署成功了。
連接到WebService服務(wù)
新建一個(gè)控制臺(tái)應(yīng)用。
然后打開(kāi)webservice地址輸入。
http://localhost:81/webservice1.asmx?wsdl
會(huì)打開(kāi)一個(gè)xml文件。
接著右鍵文件另存為,把文件保存下來(lái)。并修改文件后綴名為wsdl
。
在VS中添加,添加服務(wù)引用。選擇WCF Web Service。
這里其實(shí)可以直接輸入WebService的地址點(diǎn)擊轉(zhuǎn)到即可。當(dāng)考慮到要連接的服務(wù)在本地不一定是可以訪問(wèn)的,所以我們可以點(diǎn)擊瀏覽通過(guò)上面生成的wsdl文件來(lái)生成對(duì)應(yīng)的代碼。
添加進(jìn)來(lái)后如下圖所示,命名空間可以按照實(shí)際名稱(chēng)修改。
之后點(diǎn)擊下一步,然后點(diǎn)擊完成即可。
完成之后這里就多了兩個(gè)文件。
調(diào)用方式如下,直接實(shí)例化對(duì)應(yīng)的類(lèi),然后就可以像調(diào)用普通方法一樣,調(diào)用遠(yuǎn)程的服務(wù)接口了。
using ServiceReference1; using System; using System.Threading.Tasks; namespace TestProject { public class Program { static async Task Main(string[] args) { await Test(); } public static async Task Test() { var reference = new WebService1SoapClient(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); } } }
返回結(jié)果如下,就像調(diào)用本地方法一樣自然。
不過(guò)這里應(yīng)該有地方需要按需修改一下,在Reference.cs
文件中,遠(yuǎn)程服務(wù)地址是寫(xiě)死的。所以需要改成參數(shù)。
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration) { if ((endpointConfiguration == EndpointConfiguration.WebService1Soap)) { return new System.ServiceModel.EndpointAddress("http://localhost:81/webservice1.asmx"); } if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12)) { return new System.ServiceModel.EndpointAddress("http://localhost:81/webservice1.asmx"); } throw new System.InvalidOperationException(string.Format("找不到名稱(chēng)為“{0}”的終結(jié)點(diǎn)。", endpointConfiguration)); }
改造方法也簡(jiǎn)單。添加一個(gè)url的入?yún)ⅰ?/p>
private static System.ServiceModel.EndpointAddress GetEndpointAddress(string url,EndpointConfiguration endpointConfiguration) { if ((endpointConfiguration == EndpointConfiguration.WebService1Soap)) { return new System.ServiceModel.EndpointAddress(url); } if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12)) { return new System.ServiceModel.EndpointAddress(url); } throw new System.InvalidOperationException(string.Format("找不到名稱(chēng)為“{0}”的終結(jié)點(diǎn)。", endpointConfiguration)); }
以及引用這個(gè)方法的這里都加上url。
public WebService1SoapClient(string url, EndpointConfiguration endpointConfiguration) : base(WebService1SoapClient.GetBindingForEndpoint(endpointConfiguration), WebService1SoapClient.GetEndpointAddress(url,endpointConfiguration)) { this.Endpoint.Name = endpointConfiguration.ToString(); ConfigureEndpoint(this.Endpoint, this.ClientCredentials); }
調(diào)用的時(shí)候把Url傳進(jìn)去即可。
var url = "http://localhost:81/webservice1.asmx"; var reference = new WebService1SoapClient(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。
總結(jié)
到此這篇關(guān)于C#創(chuàng)建WebService接口并連接的文章就介紹到這了,更多相關(guān)C#創(chuàng)建WebService接口并連接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 創(chuàng)建控制臺(tái)應(yīng)用程序
這篇文章主要介紹了C# 創(chuàng)建控制臺(tái)應(yīng)用程序,在學(xué)習(xí)C#語(yǔ)言的時(shí)候,首先要學(xué)習(xí)控制臺(tái)的應(yīng)用程序,這樣才能專(zhuān)注于語(yǔ)言的學(xué)習(xí),減少學(xué)習(xí)的梯度,也有利于輸出自己需要輸出的內(nèi)容,一定要先使用控制臺(tái)的應(yīng)用程序的方式,下面就和小編一起學(xué)習(xí)該內(nèi)容吧2021-10-10C#通過(guò)IComparable實(shí)現(xiàn)ListT.sort()排序
這篇文章主要介紹了C#通過(guò)IComparable實(shí)現(xiàn)ListT.sort()排序的方法,可實(shí)現(xiàn)自定義的排序方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09C# WPF 父控件通過(guò)使用可視化樹(shù)找到子控件的示例代碼
這篇文章主要介紹了C# WPF 父控件通過(guò)使用可視化樹(shù)找到子控件的示例代碼,需要的朋友可以參考下2018-08-08解析C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用,備忘錄模式用來(lái)保存與對(duì)象有關(guān)的數(shù)據(jù)用以在將來(lái)對(duì)對(duì)象進(jìn)行復(fù)原,需要的朋友可以參考下2016-02-02C#實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類(lèi)型轉(zhuǎn)換方法詳解
這篇文章主要為大家介紹了C#中一維數(shù)組如何快速實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類(lèi)型的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04