Search code examples
c#.netwcfservice.net-core

What replaces WCF in .Net Core?


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 Framework). I then use the console application to proxy call this function within the server.

However if I use Console App (.Net Core) and a Class Library (.Net Core) the System.ServiceModel is not available. I have done some Googling but I haven't figured out what "replaces" WCF in this instance.

How do I expose a Add(int x, int y) function within a class library to a console application all within .Net Core? I see System.ServiceModel.Web, and since this is trying to be cross platform do I have to create a RESTful service?


Solution

  • 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<IComputingService>(name: "endpoint1", pipeName: "pipeName")
                  .AddTcpEndpoint<IComputingService>(name: "endpoint2", ipEndpoint: IPAddress.Loopback, port: 45684)
                  .Build()
                  .Run();
          }
      
          private static IServiceCollection ConfigureServices(IServiceCollection services)
          {
              return services
                  .AddIpc()
                  .AddNamedPipe(options =>
                  {
                      options.ThreadCount = 2;
                  })
                  .AddService<IComputingService, ComputingService>();
          }
      }
      
    4. Invoke the service from client process

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