什么可以替代.Net Core中的WCF?

编程入门 行业动态 更新时间:2024-10-22 07:31:26
本文介绍了什么可以替代.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?

本文发布于:2023-11-14 19:27:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Net   Core   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!