. NET Core Development Log--WCF Client

Source: Internet
Author: User
Tags dotnet soap

WCF as a. NET Framework3.0 is being introduced to build a service-oriented framework that plays a major role in many projects. Today, although newer technologies can replace it, the cost of refactoring the new framework for existing projects or products may not be enough to find people willing to pay.

In the. NET Core platform environment, WCF is not fully included in the migration target. The service side of WCF is shelved and only the client has been ported into. NET Core.

This means that if there is a need in. NET core, especially in a non-Windows system environment, it is not impossible to invoke an existing WCF service.

To prove with an experiment, build a solution project, add two class library projects, and a console application.

Wcfservice.contract project, which is the interface of the WCF service, the service contract.

namespace WcfService.Contract{    [ServiceContract]    public interface ICommunication    {        [OperationContract]        string Ping(string msg);    }}

The Wcfservice project is the implementation of the service.

namespace WcfService{    public class Communication : ICommunication    {        public string Ping(string msg)        {            return string.Format("Pong: {0}", msg);        }    }}

The Wcfservice.host project, which implements the hosting of the service.

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();            }        }    }}

All three of these projects use the. NET framework 4.5.2 as the target framework.

By running the Wcfservice.host application, you can start the WCF service side. Of course, this server can only run on Windows system environments. (for experimentation, it is recommended to temporarily shut down the system's firewall to avoid connectivity)

Find an environment for non-Windows systems, such as the Mac Air I'm using. Create a console application again.

dotnet new console -o WcfClientApp

To open a project with visual Studio code, it is recommended that you install the NuGet Package Manager plug-in because you need to introduce the System.ServiceModel.Http class library.

Using the shortcut key CTRL (Command) +p, enter >nuget, select the NuGet package Manager:add the package, Enter System.ServiceModel.Http, then select the latest version of the installation options, the corresponding class library will be downloaded automatically.

In addition to this class library, you need to use the DLL file of the wcfservice.contract you created earlier. Copy it to a directory and specify its location in the csproj file.

<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>

The code for the WCF client is as follows:

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();        }    }}

By running this client, you can see that the experiment was successful.

Of course, the use of WCF client on. NET core must be limited, it only supports HTTP and TCP two communication protocols, such as Namedpipe (named pipe), MSMQ, the Windows platform-specific communication protocol, is certainly not supported. However, the most commonly used is the two, so the majority of the application scenario is sufficient.

The above mentioned WCF server is not supported by. NET Core, but there is a solution if you just want to build a SOAP service.

Also on the MacOS system, create a new Web application.

dotnet new web -o SOAPApp

Install the Soapcore class library through NuGet Package Manager and introduce WcfService.dll with 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>

Then inject the required service into the startup file and increase the endpoint of the SOAP service.

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());        }    }}

To run this Web application, note that the default local address is changed to the actual URL.

In the Windows System environment, a console application is established as a client for detection.

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();        }    }}

The result of the operation is equally normal, this time the attempt to perfect the end.

. NET Core Development Log--WCF Client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.