摘要:本文主要向大家介绍了C#编程之.NET Core开发日志——WCF Client,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之.NET Core开发日志——WCF Client,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
WCF作为.NET Framework3.0就被引入的用于构建面向服务的框架在众多项目中发挥着重大作用。时至今日,虽然已有更新的技术可以替代它,但对于那些既存项目或产品,使用新框架重构的代价未必能找到人愿意买单。
而在.NET Core平台环境中,WCF也并没有被完全列入迁移目标。WCF的服务端被搁置一旁,只有客户端已被移植入.NET Core之中。
这意味着,如果有需求在.NET Core中,尤其是非Windows系统环境,调用现有的WCF服务,也并非一件不可能的事情。
以一个实验来证明,先建一个解决方案工程,再加入两个类库项目及一个控制台应用程序。
WcfService.Contract项目,这是WCF服务的接口,即服务契约。
namespace WcfService.Contract{ [ServiceContract] public interface ICommunication { [OperationContract] string Ping(string msg); } }
WcfService项目,是对服务的实现。
namespace WcfService{ public class Communication : ICommunication { public string Ping(string msg) { return string.Format(""Pong: {0}"", msg); } } }
WcfService.Host项目,实现对服务的托管。
namespace WcfService.Host{ class Program { static void Main(string[] args) { using (var host = new ServiceHost(typeof(Communication))) { host.AddServiceEndpoint(typeof(ICommunication), new BasicHttpBinding(), new Uri(""//192.168.1.2:6666"")); host.Open(); Console.WriteLine(""Service is being hosted...""); Console.Read(); } } } }
以上三个项目皆使用.NET framework 4.5.2作为目标框架。
通过运行WcfService.Host应用程序,可以将WCF服务端启动起来。当然此服务端只能运行在Windows系统环境之上。(为了实验,建议将系统的防火墙暂时关闭,以免无法连通)
再找一个非Windows系统的环境,比如我使用的Mac Air。再创建一个控制台应用程序。
dotnet new console -o WcfClientApp
用Visual Studio Code打开工程,建议安装Nuget Package Manager插件,因为这里需要引入System.ServiceModel.Http类库。
使用快捷键Ctrl(Command)+p,输入>nuget,选中Nuget Package Manager: Add Package,输入System.ServiceModel.Http,再选取最新版本的安装选项,对应的类库便会自动下载下来。
除了这个类库之外,还需要使用之前创建的WcfService.Contract的dll文件。将其复制到某个目录下,并在csproj文件指明其具体位置即可。
<Project Sdk=""Microsoft.NET.Sdk""> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=""System.ServiceModel.Http"" Version=""4.5.3""/> </ItemGroup> <ItemGroup> <Reference Include=""WcfService.Contract""> <HintPath>bin\Debug\netcoreapp2.1\WcfService.Contract.dll</HintPath> </Reference> </ItemGroup></Project>
WCF客户端的代码如下:
using System;using System.ServiceModel;using WcfService.Contract;namespace WcfClientApp{ class Program { static void Main(string[] args) { var factory = new ChannelFactory<ICommunication>( new BasicHttpBinding(), new EndpointAddress(new Uri(""//192.168.1.2:6666""))); var channel = factory.CreateChannel(); Console.WriteLine(""Ping...""); var result = channel.Ping(""Hi""); Console.WriteLine(result); ((ICommunicationObject)channel).Close(); Console.Read(); } } }
将此客户端运行起来,可以看到这个实验成功了。
当然WCF Client在.NET Core上的使用一定是有限制,其仅支持HTTP与TCP两种通信协议,如NamedPipe(命名管道),MSMQ这种Windows平台特有的通信协议,肯定是不被支持的。不过一般最常用的也就是这两种,所以大多数应用场景下也是够用了。
上面提到了WCF服务端不被.NET Core所支持,但如果只是想建一个SOAP的服务,还是有解决方案的。
同样是在macOS系统上,新建一个Web应用程序。
dotnet new web -o SOAPApp
通过Nuget Package Manager安装SoapCore类库,并将WcfService.dll与WcfService.Contract.dll一并引入。
<Project Sdk=""Microsoft.NET.Sdk.Web""> <PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <ItemGroup> <Folder Include=""wwwroot\""/> </ItemGroup> <ItemGroup> <PackageReference Include=""Microsoft.AspNetCore.App""/> <PackageReference Include=""SoapCore"" Version=""0.9.8.1""/> </ItemGroup> <ItemGroup> <Reference Include=""WcfService""> <HintPath>bin\Debug\netcoreapp2.1\WcfService.dll</HintPath> </Reference> <Reference Include=""WcfService.Contract""> <HintPath>bin\Debug\netcoreapp2.1\WcfService.Contract.dll</HintPath> </Reference> </ItemGroup></Project>
然后在Startup文件中注入所需的服务,并增加SOAP服务的端点。
namespace SOAPApp{ public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddSingleton(new Communication()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSoapEndpoint<Communication>(""/Communication.svc"", new BasicHttpBinding()); } } }
运行此Web应用程序,注意将默认的local地址改成实际的Url。
再在Windows系统环境下建立一个控制台应用程序作为客户端用于检测。
namespace WcfService.Client{ class Program { static void Main(string[] args) { var factory = new ChannelFactory<ICommunication>(new BasicHttpBinding(), new EndpointAddress(new Uri(""//192.168.1.6:5000/Communication.svc""))); var channel = factory.CreateChannel(); Console.WriteLine(""Ping...""); var result = channel.Ping(""Hi""); Console.WriteLine(result); ((ICommunicationObject)channel).Close(); Console.Read(); } } }
运行结果,同样正常,这次的的尝试完美结尾。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号