Let's understand here what alternative of WCF in .Net Core.
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:
Create service contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICalculator | |
{ | |
int Addcalc(int x, int y); | |
} |
Implement the service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator : ICalculator | |
{ | |
public int AddFloat(int x, int y) | |
{ | |
return x + y; | |
} | |
} |
Host the service in Console application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<ICalculator, Calculator>(); | |
} | |
} |
Invoke the service from client process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IpcServiceClient<ICalculator> client = new IpcServiceClientBuilder<ICalculator>() | |
.UseNamedPipe("pipeName") // or .UseTcp(IPAddress.Loopback, 45684) to invoke using TCP | |
.Build(); | |
int result = await client.InvokeAsync(x => x.Addcalc(12, 15)); |
Summary
So far we learnt What replaces WCF in .Net Core. Please give your comments if have any suggestion.