ASP.NET Core项目中调用WebService的方法

2022-04-17 16:13:32

一、前言

现实生产中,有一些比较老的系统对外提供的接口都是WebService形式的,如果是使用.NET Framework创建的项目调用WebService非常方便,网上有很多代码示例,这里不在讲解,下面我们讲解如何在ASP.NET Core项目里面调用WebService。首先我们需要创建一个WebService项目和一个ASP.NET Core WebApi项目。创建的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 从脚本中调用此 Web 服务,请取消注释以下行。     // [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 $"输入参数:{para}";        }    }}

里面分别有一个无参和有参的方法。我们在ASP.NET Core WebApi项目里面分别调用这两个方法并输出。

二、引用WebService

首先我们在创建好的ASP.NET Core WebApi项目里面添加WebService的引用。

1、在依赖项上面右键,选择“添加连接的服务”,如图所示:

// 调用TestMethod方法,不传递参数 Task<TestMethodResponse> testResponse = callClient.TestMethodAsync(new TestMethodRequest()); // 获取 string result2 = testResponse.Result.Body.TestMethodResult; // 调用TestMethod方法,并传递参数 TestMethodRequestBody body = new TestMethodRequestBody("测试TestMethod方法"); Task<TestMethodResponse> testResponsePara = callClient.TestMethodAsync(new TestMethodRequest(body)); // 获取 string result3 = testResponse.Result.Body.TestMethodResult; return $"HelloWorld方法返回值:{result1},TestMethod方法不传递参数返回值:{result2},TestMethod方法传递参数的返回值:{result3}"; } }}

我们在WebService里面定义的TestMethod方法有一个string类型的参数,调用的时候有两个重载函数,一个无参,一个有参,看一下自动生成的Reference.cs类里面的代码:

ASP.NETCore项目中调用WebService的方法

发现TestMethodRequestBody有两个构造函数:一个无参,一个有参。我们在浏览器里面调用Get方法,程序输出结果:

ASP.NETCore项目中调用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()        {            #region 调用方法1            ////创建 HTTP 绑定对象            //var binding = new BasicHttpBinding();            ////根据 WebService 的 URL 构建终端点对象,参数是提供的WebService地址            //var endpoint = new EndpointAddress(@"http://localhost:37907/CoreTest.asmx");            ////创建调用接口的工厂,注意这里泛型只能传入接口 泛型接口里面的参数是WebService里面定义的类名+Soap            //var factory = new ChannelFactory<CoreTestSoap>(binding, endpoint);            ////从工厂获取具体的调用实例            //var callClient = factory.CreateChannel();            ////调用具体的方法,这里是 HelloWorldAsync 方法            //Task<HelloWorldResponse> responseTask = callClient.HelloWorldAsync(new HelloWorldRequest());            ////获取结果            //HelloWorldResponse response = responseTask.Result;            //// 获取HelloWorld方法的返回值            //string result1 = response.Body.HelloWorldResult;            //// 调用TestMethod方法,不传递参数            //Task<TestMethodResponse> testResponse = callClient.TestMethodvwPKOGrAsync(new TestMethodRequest());            //// 获取            //string result2 = testResponse.Result.Body.TestMethodResult;            //// 调用TestMethod方法,并传递参数            //TestMethodRequestBody body = new TestMethodRequestBody("测试TestMethod方法");            //Task<TestMethodResponse> testResponsePara = callClient.TestMethodAsync(new TestMethodRequest(body));            //// 获取            //string result3 = testResponseParwww.easck.coma.Result.Body.TestMethodResult;             #endregion            #region 调用方法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;            // 调用TestMethod方法,这时必须传入参数            Task<TestMethodResponse> testResponseTask = client.TestMethodAsync("测试TestMethod方法");            // 获取TestMethod方法的返回值            string result2 = testResponseTask.Result.Body.TestMethodResult;            #endregion            return $"HelloWorld方法返回值:{result1},TestMethod方法返回值:{result2}";        }    }}

在这种方式中,调用有参的方法必须要传递参数。

程序运行结果:

ASP.NETCore项目中调用WebService的方法

如果以后WebService有更新,只需要更新添加的服务引用即可,如图所示:

ASP.NETCore项目中调用WebService的方法

到此这篇关于ASP.NET Core项目调用WebService的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。