What replaces WCF in .Net Core?

117,376

Solution 1

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

Solution 2

You can use gRPC for hosting web services inside .NET core application.

enter image description here

Introduction

  1. gRPC is a high performance, open source RPC framework initially developed by Google.
  2. The framework is based on a client-server model of remote procedure calls. A client application can directly call methods on a server application as if it was a local object.

Example

Server Code

class Program
{
    static void Main(string[] args)
    {
        RunAsync().Wait();
    }

    private static async Task RunAsync()
    {
        var server = new Grpc.Core.Server
        {
            Ports = { { "127.0.0.1", 5000, ServerCredentials.Insecure } },
            Services =
            {
                ServerServiceDefinition.CreateBuilder()
                    .AddMethod(Descriptors.Method, async (requestStream, responseStream, context) =>
                    {
                        await requestStream.ForEachAsync(async additionRequest =>
                        {
                            Console.WriteLine($"Recieved addition request, number1 = {additionRequest.X} --- number2 = {additionRequest.Y}");
                            await responseStream.WriteAsync(new AdditionResponse {Output = additionRequest.X + additionRequest.Y});
                        });
                    })
                    .Build()
            }
        };

        server.Start();

        Console.WriteLine($"Server started under [127.0.0.1:5000]. Press Enter to stop it...");
        Console.ReadLine();

        await server.ShutdownAsync();
    }
}

Client Code

class Program
{
    static void Main(string[] args)
    {
        RunAsync().Wait();
    }

    private static async Task RunAsync()
    {
        var channel = new Channel("127.0.0.1", 5000, ChannelCredentials.Insecure);
        var invoker = new DefaultCallInvoker(channel);
        using (var call = invoker.AsyncDuplexStreamingCall(Descriptors.Method, null, new CallOptions{}))
        {
            var responseCompleted = call.ResponseStream
                .ForEachAsync(async response => 
                {
                    Console.WriteLine($"Output: {response.Output}");
                });

            await call.RequestStream.WriteAsync(new AdditionRequest { X = 1, Y = 2});
            Console.ReadLine();

            await call.RequestStream.CompleteAsync();
            await responseCompleted;
        }

        Console.WriteLine("Press enter to stop...");
        Console.ReadLine();

        await channel.ShutdownAsync();
    }
}

Shared Classes between Client and Server

[Schema]
public class AdditionRequest
{
    [Id(0)]
    public int X { get; set; }
    [Id(1)]
    public int Y { get; set; }
}

[Schema]
public class AdditionResponse
{
    [Id(0)]
    public int Output { get; set; }
}

Service descriptors

using Grpc.Core;
public class Descriptors
{
    public static Method<AdditionRequest, AdditionResponse> Method =
            new Method<AdditionRequest, AdditionResponse>(
                type: MethodType.DuplexStreaming,
                serviceName: "AdditonService",
                name: "AdditionMethod",
                requestMarshaller: Marshallers.Create(
                    serializer: Serializer<AdditionRequest>.ToBytes,
                    deserializer: Serializer<AdditionRequest>.FromBytes),
                responseMarshaller: Marshallers.Create(
                    serializer: Serializer<AdditionResponse>.ToBytes,
                    deserializer: Serializer<AdditionResponse>.FromBytes));
}

Serializer/Deserializer

public static class Serializer<T>
{
    public static byte[] ToBytes(T obj)
    {
        var buffer = new OutputBuffer();
        var writer = new FastBinaryWriter<OutputBuffer>(buffer);
        Serialize.To(writer, obj);
        var output = new byte[buffer.Data.Count];
        Array.Copy(buffer.Data.Array, 0, output, 0, (int)buffer.Position);
        return output;
    }

    public static T FromBytes(byte[] bytes)
    {
        var buffer = new InputBuffer(bytes);
        var data = Deserialize<T>.From(new FastBinaryReader<InputBuffer>(buffer));
        return data;
    }
}

Output

Sample client output

Sample Server output

References

  1. https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/
  2. https://grpc.io/docs/
  3. https://grpc.io/docs/quickstart/csharp.html
  4. https://github.com/grpc/grpc/tree/master/src/csharp

Benchmarks

  1. http://csharptest.net/787/benchmarking-wcf-compared-to-rpclibrary/index.html

Solution 3

It seems, that there will be a CoreWCF project maintained by .NET Foundation with Microsoft support.

More details at Welcoming Core WCF to the .NET Foundation

Initially only netTcp and http transport will be implemented.

Solution 4

WCF does many things; it is an easy way to remote procedure calls between two applications (processes) on one machine, using named pipes; it can be a high volume internal client-server communication channel between .NET components, using binary serialization over TCPIP; or it can provide a standardised cross-technology API, e.g. via SOAP. It even has support for things like asynchronous messaging, via MSMQ.

For .NET Core, there are different replacements based on the purpose.

For cross-platform API, you would replace this with a REST service using ASP.NET.

For inter-process connections, or client-server connection, gRPC would be good, with an excellent answer given by @Gopi.

So the answer to "What replaces WCF" depends on what you are using it for.

Solution 5

There is a community repo https://github.com/CoreWCF/CoreWCF that implements some parts of WCF. You can use it to support some simple WCF services. However not all features are supported.

Share:
117,376
Sigex
Author by

Sigex

Updated on January 24, 2022

Comments

  • Sigex
    Sigex over 2 years

    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?

  • jamiebarrow
    jamiebarrow about 6 years
  • Sigex
    Sigex almost 6 years
    Nice! Might be worth updating to take advantage of the .Net core system.io.pipelines blogs.msdn.microsoft.com/dotnet/2018/07/09/…
  • Sigex
    Sigex almost 6 years
    Yea it was the auto generated proxy classes that I wanted. I am using RESTful services / RPC for this functionality
  • hal9000
    hal9000 over 5 years
    I believe this port is for communication from Core to WCF but not for writing WCF in Core.
  • user1034912
    user1034912 over 5 years
    Sorry am i missing something important here? Isn't pipes supposed to be only for same host communications?
  • Bahaa
    Bahaa over 5 years
    The linked github repository clearly says: " This repo contains the client-oriented WCF libraries that enable applications built on .NET Core to communicate with WCF services. "
  • resnyanskiy
    resnyanskiy about 5 years
    As of March 2019, this answer is more relevant. See github.com/grpc/grpc-dotnet (and ASP.NET Core updates in .NET Core 3.0).
  • joe
    joe about 5 years
    I think this is the closest answer but still, sadly , it doesn't provide any behavior or throttling support.
  • Chris F Carroll
    Chris F Carroll about 5 years
    Yes, what you're missing is that this briefly demonstrates that IpcServiceFramework, like WCF, allows you to switch kind-of-seamlessly between different messaging technologies.
  • Samuel
    Samuel almost 5 years
    Be also aware that as of now gRPC does not compile against the .net native tool chain in VS 2019 (16.0.2) and therefore won't work with UWP.
  • Jeremy
    Jeremy almost 5 years
    WCF might be considered windows specific in some of the protocols that it abstracts, but SOAP services are not. How would one create a SOAP web service in .net core?
  • Wiktor Zychla
    Wiktor Zychla over 4 years
    WCF is not a "windows specific technology" but rather, a .NET way of implementing the interoperable SOAP/WSDL stack. A web framework that doesn't support it has no easy way to connect to thousand of already implemented services. Definitely something that has to be addressed in future .NET core.
  • orellabac
    orellabac over 4 years
    This repo is just for the client libraries
  • gerard
    gerard over 4 years
    Note: the author of this project wrote the following comment: "Guys, for personal reason I do have no time to maintain this project since a few months. Meantime .NET Core 3.0 is released with gRPC feature." (github.com/jacqueskang/IpcServiceFramework/issues/…). See the second answer for gRPC.
  • Cyanfish
    Cyanfish over 4 years
    If you're looking for named pipe support, I wrote a gRPC transport: github.com/cyanfish/grpc-dotnet-namedpipes
  • GafferMan2112
    GafferMan2112 about 4 years
    Note that (as of 2020-04-06) grpc-dotnet doesn't have packages for ARM.
  • Bruno Brant
    Bruno Brant almost 4 years
    "WCF is not supported in .NET Core since it's a Windows specific technology while .NET Core is supposed to be cross-platform." And then, there's WPF. :(
  • Aron
    Aron almost 4 years
    Why did you decide to use a Lambda for invoke rather than DispatchProxy or even IL Emit? You could construct a transparent Proxy object with either of these options. Since you are going over network the reflection perf hits would be swamped by the latency anyway.
  • Latency
    Latency almost 4 years
    Also note, that it makes use of the HTTP/2 protocol. blog.netsil.com/…
  • user1034912
    user1034912 over 3 years
    This is a misleading answer. Microsoft has only ported the wcf client. Wcf host or servicehost is not available and they have no intention to do so. I learned this the hard way. gRPC is the way to go
  • user1034912
    user1034912 over 3 years
    Asp core web api does not support duplex communications on a single port like how wcf does.
  • Access Denied
    Access Denied over 3 years
    @user1034912 you are not correct. CoreWCF is lightweight WCF server which is ported to .NET core. It has limitations, but for some cases it is a good choice.
  • user1034912
    user1034912 over 3 years
    Yes, only if you are a Consuming Client, there is no servicehost implementation
  • Access Denied
    Access Denied over 3 years
    @user1034912 No, server side is available. github.com/CoreWCF/CoreWCF/blob/master/src/Samples/…
  • Access Denied
    Access Denied over 3 years
  • user1034912
    user1034912 over 3 years
    Wow this is great, didn't knew about this. Thanks, It however doesn't support WebHttpBinding
  • orellabac
    orellabac over 3 years
    The client part if supported up to certain extend: mobilize.net/blog/can-i-use-wcf-in-.net-core-3.1-or-.net5
  • DoronG
    DoronG about 3 years
    @user1034912, you are correct. for that, you should probably use a websocket/gRPC
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.