What replaces WCF in .Net Core?

后端 未结 9 541
逝去的感伤
逝去的感伤 2020-11-30 19:27

I am used to creating a .Net Framework console application and exposing a Add(int x, int y) function via a WCF service from scratch with Class Library (.Net Fra

9条回答
  •  感情败类
    2020-11-30 19:38

    WCF is not supported in .NET Core since it's a Windows specific technology and .NET Core is supposed to be cross-platform.

    If you are implementing inter-process communication consider trying the IpcServiceFramework project.

    It allows creating services in WCF style like this:

    1. Create service contract

      public interface IComputingService
      {
          float AddFloat(float x, float y);
      }
      
    2. Implement the service

      class ComputingService : IComputingService
      {
          public float AddFloat(float x, float y)
          {
              return x + y;
          }
      }
      
    3. Host the service in Console application

      class Program
      {
          static void Main(string[] args)
          {
              // configure DI
              IServiceCollection services = ConfigureServices(new ServiceCollection());
      
              // build and run service host
              new IpcServiceHostBuilder(services.BuildServiceProvider())
                  .AddNamedPipeEndpoint(name: "endpoint1", pipeName: "pipeName")
                  .AddTcpEndpoint(name: "endpoint2", ipEndpoint: IPAddress.Loopback, port: 45684)
                  .Build()
                  .Run();
          }
      
          private static IServiceCollection ConfigureServices(IServiceCollection services)
          {
              return services
                  .AddIpc()
                  .AddNamedPipe(options =>
                  {
                      options.ThreadCount = 2;
                  })
                  .AddService();
          }
      }
      
    4. Invoke the service from client process

      IpcServiceClient client = new IpcServiceClientBuilder()
          .UseNamedPipe("pipeName") // or .UseTcp(IPAddress.Loopback, 45684) to invoke using TCP
          .Build();
      
      float result = await client.InvokeAsync(x => x.AddFloat(1.23f, 4.56f));
      

提交回复
热议问题