C#創(chuàng)建WebService接口并連接的全過程
創(chuàng)建WebService項(xiàng)目
首先安裝下.NET Framework4.6.2-4.7.1開發(fā)工具。
然后就是新建 ASP.NET Web應(yīng)用程序 項(xiàng)目。
輸入項(xiàng)目名稱WebServiceDemo
選擇空,然后先去掉HTTPS配置。
項(xiàng)目創(chuàng)建好之后,開始添加asmx
文件.
添加好之后在添加一個有參數(shù)的名為Hello
的方法。代碼如下圖。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebServiceDemo { /// <summary> /// WebService1 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消注釋以下行。 // [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; } } }
然后就可以直接啟動了,也可以發(fā)布到IIS中啟動。這里先發(fā)布到IIS,一會在新建一個控制臺項(xiàng)目用于連接到該服務(wù)。
發(fā)布好之后在IIS中添加網(wǎng)站,并綁定端口號為81.然后就可以啟動了。
直接啟動的話可能會報下面的錯誤,這是因?yàn)闆]有設(shè)置起始頁。
可以直接輸入地址訪問。
http://localhost:81/webservice1.asmx
也可以在IIS默認(rèn)文檔中添加webservice1.asmx
文件。下次在瀏覽就可以直接打開了。
出現(xiàn)下圖的頁面,就表示服務(wù)已經(jīng)部署成功了。
連接到WebService服務(wù)
新建一個控制臺應(yīng)用。
然后打開webservice地址輸入。
http://localhost:81/webservice1.asmx?wsdl
會打開一個xml文件。
接著右鍵文件另存為,把文件保存下來。并修改文件后綴名為wsdl
。
在VS中添加,添加服務(wù)引用。選擇WCF Web Service。
這里其實(shí)可以直接輸入WebService的地址點(diǎn)擊轉(zhuǎn)到即可。當(dāng)考慮到要連接的服務(wù)在本地不一定是可以訪問的,所以我們可以點(diǎn)擊瀏覽通過上面生成的wsdl文件來生成對應(yīng)的代碼。
添加進(jìn)來后如下圖所示,命名空間可以按照實(shí)際名稱修改。
之后點(diǎn)擊下一步,然后點(diǎn)擊完成即可。
完成之后這里就多了兩個文件。
調(diào)用方式如下,直接實(shí)例化對應(yīng)的類,然后就可以像調(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)用本地方法一樣自然。
不過這里應(yīng)該有地方需要按需修改一下,在Reference.cs
文件中,遠(yuǎn)程服務(wù)地址是寫死的。所以需要改成參數(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("找不到名稱為“{0}”的終結(jié)點(diǎn)。", endpointConfiguration)); }
改造方法也簡單。添加一個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("找不到名稱為“{0}”的終結(jié)點(diǎn)。", endpointConfiguration)); }
以及引用這個方法的這里都加上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)用的時候把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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#通過IComparable實(shí)現(xiàn)ListT.sort()排序
這篇文章主要介紹了C#通過IComparable實(shí)現(xiàn)ListT.sort()排序的方法,可實(shí)現(xiàn)自定義的排序方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09解析C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用,備忘錄模式用來保存與對象有關(guān)的數(shù)據(jù)用以在將來對對象進(jìn)行復(fù)原,需要的朋友可以參考下2016-02-02C#實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型轉(zhuǎn)換方法詳解
這篇文章主要為大家介紹了C#中一維數(shù)組如何快速實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04