欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法

 更新時間:2022年03月21日 09:00:05   投稿:yangbin  
這篇文章介紹了ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、前言

現(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論