ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法
一、前言
現(xiàn)實(shí)生產(chǎn)中,有一些比較老的系統(tǒng)對外提供的接口都是WebService形式的,如果是使用.NET Framework創(chuàng)建的項(xiàng)目調(diào)用WebService非常方便,網(wǎng)上有很多代碼示例,這里不在講解,下面我們講解如何在ASP.NET Core項(xiàng)目里面調(diào)用WebService。首先我們需要創(chuàng)建一個WebService項(xiàng)目和一個ASP.NET Core WebApi項(xiàng)目。創(chuàng)建的WebService代碼如下:
using System.Web.Services; namespace CoreCallWebServiceTest { /// <summary> /// CoreTest 的摘要說明 /// </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 CoreTest : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } /// <summary> /// /// </summary> /// <param name="para"></param> /// <returns></returns> [WebMethod] public string TestMethod(string para) { return $"輸入?yún)?shù):{para}"; } } }
里面分別有一個無參和有參的方法。我們在ASP.NET Core WebApi項(xiàng)目里面分別調(diào)用這兩個方法并輸出。
二、引用WebService
首先我們在創(chuàng)建好的ASP.NET Core WebApi項(xiàng)目里面添加WebService的引用。
1、在依賴項(xiàng)上面右鍵,選擇“添加連接的服務(wù)”,如圖所示:
2、選擇“Microsoft WCF Web Service Referenct Provider”,如圖所示:
3、添加服務(wù)引用。如圖所示:
配置完以后,點(diǎn)擊“下一步”,去掉重新使用引用的程序集中的類型簽名的復(fù)選框。如果不去掉復(fù)選框,生成的時候可能會報錯。
直接點(diǎn)擊“完成”按鈕即可。慢慢等待配置完成:
配置完成界面如圖所示:
這樣就添加完了,下面開始在代碼里面調(diào)用提供的WebService里面的方法。
三、在代碼中調(diào)用WebService
我們添加一個名為Test的控制器,里面有一個Get方法,返回WebService里面兩個方法的返回值,代碼如下:
using System.ServiceModel; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using TestWebService; namespace AspNetCoreDemo.Controllers { [Route("api/Test")] [ApiController] public class TestController : ControllerBase { [HttpGet] public string Get() { //創(chuàng)建 HTTP 綁定對象 var binding = new BasicHttpBinding(); //根據(jù) WebService 的 URL 構(gòu)建終端點(diǎn)對象,參數(shù)是提供的WebService地址 var endpoint = new EndpointAddress(@"http://localhost:37907/CoreTest.asmx"); //創(chuàng)建調(diào)用接口的工廠,注意這里泛型只能傳入接口 泛型接口里面的參數(shù)是WebService里面定義的類名+Soap var factory = new ChannelFactory<CoreTestSoap>(binding, endpoint); //從工廠獲取具體的調(diào)用實(shí)例 var callClient = factory.CreateChannel(); //調(diào)用具體的方法,這里是 HelloWorldAsync 方法 Task<HelloWorldResponse> responseTask = callClient.HelloWorldAsync(new HelloWorldRequest()); //獲取結(jié)果 HelloWorldResponse response = responseTask.Result; // 獲取HelloWorld方法的返回值 string result1 = response.Body.HelloWorldResult; // 調(diào)用TestMethod方法,不傳遞參數(shù) Task<TestMethodResponse> testResponse = callClient.TestMethodAsync(new TestMethodRequest()); // 獲取 string result2 = testResponse.Result.Body.TestMethodResult; // 調(diào)用TestMethod方法,并傳遞參數(shù) TestMethodRequestBody body = new TestMethodRequestBody("測試TestMethod方法"); Task<TestMethodResponse> testResponsePara = callClient.TestMethodAsync(new TestMethodRequest(body)); // 獲取 string result3 = testResponse.Result.Body.TestMethodResult; return $"HelloWorld方法返回值:{result1},TestMethod方法不傳遞參數(shù)返回值:{result2},TestMethod方法傳遞參數(shù)的返回值:{result3}"; } } }
我們在WebService里面定義的TestMethod方法有一個string類型的參數(shù),調(diào)用的時候有兩個重載函數(shù),一個無參,一個有參,看一下自動生成的Reference.cs類里面的代碼:
發(fā)現(xiàn)TestMethodRequestBody有兩個構(gòu)造函數(shù):一個無參,一個有參。我們在瀏覽器里面調(diào)用Get方法,程序輸出結(jié)果:
除了上面的代碼,也可以使用下面的代碼進(jìn)行調(diào)用:
using System.ServiceModel; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using TestWebService; namespace AspNetCoreDemo.Controllers { [Route("api/Test")] [ApiController] public class TestController : ControllerBase { [HttpGet] public string Get() { #region 調(diào)用方法1 ////創(chuàng)建 HTTP 綁定對象 //var binding = new BasicHttpBinding(); ////根據(jù) WebService 的 URL 構(gòu)建終端點(diǎn)對象,參數(shù)是提供的WebService地址 //var endpoint = new EndpointAddress(@"http://localhost:37907/CoreTest.asmx"); ////創(chuàng)建調(diào)用接口的工廠,注意這里泛型只能傳入接口 泛型接口里面的參數(shù)是WebService里面定義的類名+Soap //var factory = new ChannelFactory<CoreTestSoap>(binding, endpoint); ////從工廠獲取具體的調(diào)用實(shí)例 //var callClient = factory.CreateChannel(); ////調(diào)用具體的方法,這里是 HelloWorldAsync 方法 //Task<HelloWorldResponse> responseTask = callClient.HelloWorldAsync(new HelloWorldRequest()); ////獲取結(jié)果 //HelloWorldResponse response = responseTask.Result; //// 獲取HelloWorld方法的返回值 //string result1 = response.Body.HelloWorldResult; //// 調(diào)用TestMethod方法,不傳遞參數(shù) //Task<TestMethodResponse> testResponse = callClient.TestMethodAsync(new TestMethodRequest()); //// 獲取 //string result2 = testResponse.Result.Body.TestMethodResult; //// 調(diào)用TestMethod方法,并傳遞參數(shù) //TestMethodRequestBody body = new TestMethodRequestBody("測試TestMethod方法"); //Task<TestMethodResponse> testResponsePara = callClient.TestMethodAsync(new TestMethodRequest(body)); //// 獲取 //string result3 = testResponsePara.Result.Body.TestMethodResult; #endregion #region 調(diào)用方法2 BasicHttpBinding binding = new BasicHttpBinding(); EndpointAddress address = new EndpointAddress("http://localhost:37907/CoreTest.asmx"); CoreTestSoapClient client = new CoreTestSoapClient(binding, address); Task<HelloWorldResponse> responseTask = client.HelloWorldAsync(); HelloWorldResponse response = responseTask.Result; // 獲取HelloWorld方法的返回值 string result1 = response.Body.HelloWorldResult; // 調(diào)用TestMethod方法,這時必須傳入?yún)?shù) Task<TestMethodResponse> testResponseTask = client.TestMethodAsync("測試TestMethod方法"); // 獲取TestMethod方法的返回值 string result2 = testResponseTask.Result.Body.TestMethodResult; #endregion return $"HelloWorld方法返回值:{result1},TestMethod方法返回值:{result2}"; } } }
在這種方式中,調(diào)用有參的方法必須要傳遞參數(shù)。
程序運(yùn)行結(jié)果:
如果以后WebService有更新,只需要更新添加的服務(wù)引用即可,如圖所示:
到此這篇關(guān)于ASP.NET Core項(xiàng)目調(diào)用WebService的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core在WebApi項(xiàng)目中使用Cookie
- ASP.NET Core使用HttpClient調(diào)用WebService
- 創(chuàng)建ASP.NET?Core?Web應(yīng)用程序并介紹項(xiàng)目模板
- ASP.NET?Core命名空間System.Text.Encodings.Web介紹
- asp.net core webapi文件上傳功能的實(shí)現(xiàn)
- 在IIS上部署ASP.NET Core Web API的方法步驟
- ASP.NET Core實(shí)現(xiàn)自定義WebApi模型驗(yàn)證詳解
- ASP.NET Core Web中使用AutoMapper進(jìn)行對象映射
相關(guān)文章
在WinForm和WPF中使用GMap.Net地圖插件簡單教程
GMap.NET是一個強(qiáng)大、免費(fèi)、跨平臺、開源的.NET控件,它在Windows Forms 和WPF環(huán)境中能夠使用來自Google, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac等地圖,下面看一下使用方法2013-12-12Windows虛擬主機(jī)與VPS如何實(shí)現(xiàn)301重定向(asp.net)
301重定向應(yīng)該是研究SEO必須掌握的技術(shù)。如果你是剛接觸SEO的菜鳥,想了解什么是301重定向,請看《html實(shí)現(xiàn)301重定向的方法》一文,我在該篇隨筆中引用了Google網(wǎng)站站長工具對301重定向的解釋2011-12-12ASP.NET頁面借助IFrame提交表單數(shù)據(jù)所遇到問題的解決方法分享
ASP.NET頁面借助IFrame提交表單數(shù)據(jù)所遇到問題的解決方法分享,碰到同樣問題的朋友可以參考下。2011-10-10Asp.Net其他頁面如何調(diào)用Web用戶控件寫的分頁
這篇文章主要介紹了Asp.Net其他頁面如何調(diào)用Web用戶控件寫的分頁,需要的朋友可以參考下2014-05-05.NET Core API之格式化輸出對象OutputFormatter
這篇文章介紹了.NET Core API之格式化輸出對象OutputFormatter,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04ASP.NET筆記之 Request 、Response 與Server的使用
本篇文章小編為大家介紹,ASP.NET筆記之 Request 、Response 與Server的使用。需要的朋友參考下2013-04-04ASP.NET Core實(shí)現(xiàn)文件上傳和下載
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core實(shí)現(xiàn)文件上傳和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07