.NET Core开发日志——WCF Client
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();
}
}
}
运行结果,同样正常,这次的的尝试完美结尾。

.NET Core开发日志——WCF Client的更多相关文章
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- .NET Core开发日志——Entity Framework与PostgreSQL
Entity Framework在.NET Core中被命名为Entity Framework Core.虽然一般会用于对SQL Server数据库进行数据操作,但其实它还支持其它数据库,这里就以Po ...
- .NET Core开发日志——RequestDelegate
本文主要是对.NET Core开发日志--Middleware的补遗,但是会从看起来平平无奇的RequestDelegate开始叙述,所以以其作为标题,也是合情合理. RequestDelegate是 ...
- .NET Core开发日志——从搭建开发环境开始
.NET Core自2016年推出1.0版本开始,到目前已是2.1版本,在其roadmap计划里明年更会推出3.0版本,发展不可不谓之迅捷.不少公司在经过一个谨慎的观望期后,也逐步开始将系统升级至最新 ...
- .NET Core开发日志——结构化日志
在.NET生态圈中,最早被广泛使用的日志库可能是派生自Java世界里的Apache log4net.而其后来者,莫过于NLog.Nlog与log4net相比,有一项较显著的优势,它支持结构化日志. 结 ...
- .NET Core开发日志——Edge.js
最近在项目中遇到这样的需求:要将旧有系统的一部分业务逻辑集成到新的自动化流程工具中.这套正在开发的自动化工具使用的是C#语言,而旧有系统的业务逻辑则是使用AngularJS在前端构建而成.所以最初的考 ...
- .NET Core开发日志——Linux版本的SQL Server
SQL Server 2017版本已经可以在Linux系统上安装,但我在尝试.NET Core跨平台开发的时候使用的是Mac系统,所以这里记录了在Mac上安装SQL Server的过程. 最新的SQL ...
- .NET Core开发日志——Filter
ASP.NET Core MVC中的Filter作用是在请求处理管道的某些阶段之前或之后可以运行特定的代码. Filter特性在之前的ASP.NET MVC中已经出现,但过去只有Authorizati ...
- .NET Core开发日志——Model Binding
ASP.NET Core MVC中所提供的Model Binding功能简单但实用,其主要目的是将请求中包含的数据映射到action的方法参数中.这样就避免了开发者像在Web Forms时代那样需要从 ...
随机推荐
- epoll源码分析
epoll源码分析 最近在使用libev过程中遇到一个场景:一个fd从一个ev_loop迁移到另一个ev_loop,会出现这个fd同时存在两个epoll的瞬间.不禁要问了,一个fd同时被两个epoll ...
- PHP测试Mysql数据库连接
<?php $link = mysqli_connect('localhost', 'username', 'password'); if (!$link) { die('Could not c ...
- 转 全面理解Javascript闭包和闭包的几种写法及用途
转自:http://www.cnblogs.com/yunfeifei/p/4019504.html 好久没有写博客了,过了一个十一长假都变懒了,今天总算是恢复状态了.好了,进入正题,今天来说一说ja ...
- System Monitor for Mac(系统监控工具)破解版安装
1.软件简介 System Monitor 是 macOS 系统上的一款非常实用的 Mac 系统工具,System Monitor for mac 是一款六合一应用,您可以同时获得 CPU.RA ...
- Apache Spark 2.2.0 正式发布
本章内容: 待整理 参考文献: Apache Spark 2.2.0正式发布 Spark Release 2.2.0
- python uuid 介绍
1. 背景知识: UUID: 通用唯一标识符 ( Universally Unique Identifier ), 对于所有的UUID它可以保证在空间和时间上的唯一性. 它是通过MAC地址, 时间戳, ...
- 使用ffmpeg 推流
1.编译ffmpeg http://www.linuxidc.com/Linux/2014-11/109840.htm http://www.linuxidc.com/Linux/2013-02/78 ...
- Xcode 插件优缺点对照(推荐 20 款插件)
Xcode 插件优缺点对照(推荐 20 款插件) 2016-01-22 06:16 编辑: lansekuangtu 分类:iOS开发 来源:董铂然 的博客 28 13527 /XCode/" ...
- epoll的由来
reference https://www.zhihu.com/question/20122137 感谢 @静海听风 @蓝形参 数据流有两个重要的参与者: 1.往流中写入数据者 2.从流中读取数据者 ...
- packetfence 7.2网络准入部署(一)
packetfence 是一款内网准入软件,刚开始研究的时候也是一脸懵逼,资料少的可怜,前后玩了几个月,中途很多次都想放弃了,填完了很多坑,最后也算是成功了 好了,今天就讲一下packetfence所 ...