什么可以替代.Net Core中的WCF? [英] What replaces WCF in .Net Core?

查看:1664
本文介绍了什么可以替代.Net Core中的WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于创建一个.Net Framework控制台应用程序,并通过WCF服务从头开始使用类库()公开 Add(int x,int y)函数。网络框架)。然后,我使用控制台应用程序在服务器内代理调用此功能。

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.

但是,如果我使用控制台应用程序(.Net Core)和类库(.Net Core) System.ServiceModel不可用。我做了一些谷歌搜索,但是在这种情况下我还没有弄清楚是什么替代了 WCF。

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.

如何公开 Add(int类库中的x,int y)函数到.Net Core中所有的控制台应用程序?我看到了System.ServiceModel.Web,由于这是跨平台的,所以我必须创建一个RESTful服务吗?

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?

推荐答案

.NET Core不支持WCF,因为它是Windows特定的技术,而.NET Core应该是跨平台的。
如果要实现进程间通信,请考虑尝试此项目
它允许以WCF样式创建服务:

WCF is not supported in .NET Core since it's a Windows specific technology while .NET Core is supposed to be cross-platform. If you are implementing inter-process communication consider trying this project out. It allows creating services in WCF style:

步骤1-创建服务合同

public interface IComputingService
{
    float AddFloat(float x, float y);
}

步骤2:实施服务

class ComputingService : IComputingService
{
    public float AddFloat(float x, float y)
    {
        return x + y;
    }
}

第3步-在控制台应用程序中托管服务

Step 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-从客户端进程调用服务

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

这篇关于什么可以替代.Net Core中的WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆