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("http://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("http://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("http://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();
- }
- }
- }
运行结果, 同样正常, 这次的的尝试完美结尾.
来源: https://www.cnblogs.com/kenwoo/p/9362763.html