十九、.net core使用SoapCore开发webservice接口,以及使用HttpClientFactory动态访问webservice接口

 

使用SoapCore实现在.net core平台下开发webservice;以及使用HttpClientFactory动态访问webservice

首先,需要在包项目下面引用SoapCore

 

 

 

然后新建项目Wsk.Core.WebService,用于开发webservice有关功能。

新项目下,需要先引用package项目,然后新建一个IWeskyWS接口,以及提供了三个Hello方法(webservice有可能不支持重载,如果后面无法进行服务引用,可以更改为Hello1Hello2Hello3),用于实验使用。其他介绍,如下图标注所示:

 

 

该部分代码:

[ServiceContract]
    public interface IWeskyWS
    {
        [OperationContract]
        string Hello1();
        [OperationContract]
        string Hello2(string name);
        [OperationContract]
        string Hello3(NameInfo info);
    }

    public class WeskyWS : IWeskyWS
    { 
        public string Hello1()
        {
            return "Hello";
        }
        public string Hello2(string name)
        {
            return $"Hello, {name}";
        }
        public string Hello3(NameInfo info)
        {
            return $"Hello,{info.Name}, Age is {info.Age}";
        }
    }

    [DataContract]
    public class NameInfo
    {
        [DataMember]
        public int Age { get; set; }
        [DataMember]
        public string Name { get; set; }
    }

View Code

 

现在转到启动项目下,引用该项目。然后在启动项里面,添加服务注入:

 

 

Configure下,添加UseSoapEndpoint,以及有关注释,如图注释部分:

 

 

 

启动程序,并且在浏览器下指定对应的asmx地址,如果有提示下方的xml文档,则代表启动成功。

 

 

现在咱们新建一个基于.net framework的控制台项目,用来做测试使用。

 

 

创建完毕以后,结构如下:

 

 

现在通过引用服务的方式进行引用一下:

 

 

Main方法下面调用webservice,并打印,结果如下:

 

 

注意有个坑:使用SoapCore开发的该Webservice,目前只能通过添加服务引用的方式被识别。使用动态访问方式,会无法访问。如果其他小伙伴解决了该问题,欢迎留言。

 

接下来提供一个简单的使用.net core通过HttpClientFactory来访问Webservice的方法。

注意还有一个坑:该方法目前仅针对于webservice方法参数不存在实体类的情况下。如果是复杂数据,目前暂时不支持,或者是我当前未找到行之有效的方法,也欢迎各位大佬留言评论,提供更加有效的法子。

 

由于上面使用soapCore开发的webservice目前只能被服务引用,所以此处不对其做动态访问测试有兴趣的可以自行尝试。我先创建一个使用.net framework开发的webservice

新建一个Asp.Net Web应用程序,配置如下图:

 

 

创建以后,添加一个新建项,选择web服务,用以开发webservice测试方法:

 

 

创建成功,以后,结构如下图,以及会有一个默认的HelloWorld方法。

 

 

现在加点测试方法,带一个参数的Hello1,以及带两个参数的Hello2

 

 

运行以后,如果有以下页面,说明该webservice开发成功:

 

 

现在切换回Wsk.Core项目,在启动项目的控制器里面,新建一个webapi,,用来触发访问webservice的方法,进行有关验证。先添加HttpClientFactory的依赖注入:

 

 

在此之前,还需要在启动项里面,添加对HttpClient的注册:

 

 

现在在新增的webapi里面,做一些访问webservice的实现。先新建一个动态访问webservice的方法:

 

 

方法代码:

  private  String CallWebservice(string url, Dictionary<string, string> dictionary)
        {
            HttpContent content;
            if (dictionary != null)
            {
                content = new FormUrlEncodedContent(dictionary);
            }
            else
            {
                content = new StringContent("");
            }

            string result = string.Empty;

            try {
                using (HttpClient client = _httpClientFactory.CreateClient()) {
                    using (var response = client.PostAsync(url,content).Result) { 
                        if(response.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            result = response.Content.ReadAsStringAsync().Result;
                            
                            //XmlDocument xml = new XmlDocument();
                            //xml.LoadXml(result);
                            //result = xml.InnerText;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                result = $"Error:{ex.Message}";
            }
            return result;
        }

View Code

其中,注释部分是用于获取xml内数据使用的,为了看完整的数据,所以做了注释。有兴趣的可以打开注释进行尝试。

 

然后在TestCallWS这个api下面对以上三个webservice方法进行访问:

 

 

该webapi代码:

 [HttpPost]
        public IActionResult TestCallWS()
        {

            string url = "//localhost:8435/WeskyService.asmx/";
            
            
            string method = "HelloWorld";
            string wsUrl = $"{url}{method}";
            string value1 = CallWebservice(wsUrl, null);

            Dictionary<string, string> dic = new Dictionary<string, string>();

            method = "Hello1";
            wsUrl = $"{url}{method}";
            dic.Add("name", "Wesky");
            string value2 = CallWebservice(wsUrl, dic);

            dic = new Dictionary<string, string>();
            method = "Hello2";
            wsUrl = $"{url}{method}";
            dic.Add("age", "3");
            dic.Add("name", "WESKY");
            string value3 = CallWebservice(wsUrl, dic);

            return Ok($"{value1}\n{value2}\n{value3}");
        }

View Code

 

 

启动程序,并且在swagger上面进行调用,看看结果:

 

 

访问成功,教程结束。

 

以上写法也不是最好的写法,以及使用.net core开发webservice如何能够被动态访问、以及在.net core上如何动态访问带实体参数的方法,目前还需要进一步探讨。也欢迎大佬们踊跃提供可行的技术方向。感谢大家抽时间看完该文章,希望对大家能有一点帮助。

先前也有一期使用HttpClientHttpWebRequest进行访问webapi的文章,如果有兴趣也可以莅临指导://www.cnblogs.com/weskynet/p/14856130.html

 

到此完毕,谢谢观看。