什么取代了 .Net Core 中的 WCF? [英] What replaces WCF in .Net Core?

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

问题描述

我习惯于使用类库 (.Net Framework) 从头开始​​创建 .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 is not supported in .NET Core since it's a Windows specific technology and .NET Core is supposed to be cross-platform.

如果您正在实施进程间通信,请考虑尝试 IpcServiceFramework 项目.

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

它允许像这样以 WCF 样式创建服务:

It allows creating services in WCF style like this:

  1. 创建服务合同

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

  • 实施服务

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

  • 在控制台应用程序中托管服务

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

  • 从客户端进程调用服务

    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天全站免登陆