由于 .NET Remoting 已从 .NET Core 框架中删除,我尝试使用WCF 库中的 NetTcpBinding,但它不包含在 .NET Core 中。
我可以使用其他 TCPChannel 的模拟吗?
由于 .NET Remoting 已从 .NET Core 框架中删除,我尝试使用WCF 库中的 NetTcpBinding,但它不包含在 .NET Core 中。
我可以使用其他 TCPChannel 的模拟吗?
我会尝试采用不同的 RPC 框架——理想情况下是平台中立的框架,而不是与 .NET 紧密耦合的框架。
有很多可用的选项。就在我的脑海中:
这些只是示例 - 有大量可用的 RPC 和类似 RPC 的框架。这些都不会像使用远程处理那样“透明”,但是:
如果您有一个基于 .NET Remoting 的大型代码库,那么切换到 WebAPI 或 gRPC 可能会导致重写一半的应用程序。
CoreRemoting(MIT 许可)可能是一种替代方案:https ://github.com/theRainbird/CoreRemoting 它可以将基于 .NET Remoting 的客户端/服务器应用程序迁移到 .NET Core / .NET 5。与 gRPC 或 WebAPI 相比,CoreRemoting 的过程与 .NET Remoting 非常相似。.NET 对象之间只进行远程方法调用。不需要像 WebAPI 那样将调用转换为 HTTP 调用(使用字符串连接构建 URL)。客户端和服务器之间的接口是在共享的 .NET 程序集中定义的,而不是像 gRPC 那样使用特殊的接口语言。事件和回调是开箱即用的,可以以自然的方式为 C# 开发人员使用(与 gRPC 更复杂的流式处理方法相比)。
以下示例显示了如何使用 CoreRemoting 创建简单的客户端/服务器聊天应用程序。
共享合同组装
namespace HelloWorld.Shared
{
public interface ISayHelloService
{
event Action<string, string> MessageReceived;
void Say(string name, string message);
}
}
服务器
using System;
using CoreRemoting;
using CoreRemoting.DependencyInjection;
using HelloWorld.Shared;
namespace HelloWorld.Server
{
public class SayHelloService : ISayHelloService
{
// Event to notify clients when users post new chat messages
public event Action<string, string> MessageReceived;
// Call via RPC to say something in the chat
public void Say(string name, string message)
{
MessageReceived?.Invoke(name, message);
}
}
public static class HelloWorldServer
{
static void Main(string[] args)
{
using var server = new RemotingServer(new ServerConfig()
{
HostName = "localhost",
NetworkPort = 9090,
RegisterServicesAction = container =>
{
// Make SayHelloSevice class available for RPC calls from clients
container.RegisterService<ISayHelloService, SayHelloService>(ServiceLifetime.Singleton);
}
});
server.Start();
Console.WriteLine("Server is running.");
Console.ReadLine();
}
}
}
客户
using System;
using CoreRemoting;
using HelloWorld.Shared;
namespace HelloWorld.Client
{
public static class HelloWorldClient
{
static void Main(string[] args)
{
using var client = new RemotingClient(new ClientConfig()
{
ServerHostName = "localhost",
ServerPort = 9090
});
client.Connect();
// Create a proxy of the remote service, which behaves almost like a regular local object
var proxy = client.CreateProxy<ISayHelloService>();
// Receive chat messages send by other remote users by event
proxy.MessageReceived += (senderName, message) =>
Console.WriteLine($"\n {senderName} says: {message}\n");
Console.WriteLine("What's your name?");
var name = Console.ReadLine();
Console.WriteLine("\nEntered chat. Type 'quit' to leave.");
bool quit = false;
while (!quit)
{
var text = Console.ReadLine();
if (text != null && text.Equals("quit", StringComparison.InvariantCultureIgnoreCase))
quit = true;
else
{
// Post a new chat message
proxy.Say(name, text);
}
}
}
}
}
CoreRemoting 仅适用于 .NET 到 .NET。如果您需要与 Javascript、Java、Python 等进行通信,那么它不是正确的工具。但是,如果您只想在纯 .NET 环境中进行 RPC,并且希望以一种舒适的方式进行,那么 CoreRemoting 可能会很有帮助。
我想指出,我是 CoreRemoting 项目的开发人员。