zoukankan      html  css  js  c++  java
  • .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>binDebug
    etcoreapp2.1WcfService.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>binDebug
    etcoreapp2.1WcfService.dll</HintPath>
        </Reference>
        <Reference Include="WcfService.Contract">
          <HintPath>binDebug
    etcoreapp2.1WcfService.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();
            }
        }
    }
    

    运行结果,同样正常,这次的的尝试完美结尾。

  • 相关阅读:
    JS中的各种函数
    JS循环 && JS语法
    对JS基础学习的重新理解
    vue系列---【vue项目如何使用less语法?】
    vue系列---【vue项目如何利用图形化界面安装axios依赖?】
    vue系列---【vue项目如何使用图形化界面自动安装element-ui,实现按需导入?】
    vue系列---【如何使用vue ui图形化界面创建一个vue项目?(注意:适用于3.0.x版本)】
    vue系列---【vue项目如何使用element-ui?】
    vue系列---【如何安装vue脚手架?】
    Oracle系列---【PL/SQL Developer如何连接远程oracle数据库?】
  • 原文地址:https://www.cnblogs.com/kenwoo/p/9362763.html
Copyright © 2011-2022 走看看