【发布时间】:2018-04-01 12:00:10
【问题描述】:
由于 .NET Remoting 已从 .NET Core 框架中移除,我尝试使用 NetTcpBinding from the WCF library,但它未包含在 .NET Core 中。
我可以使用其他 TCPChannel 的类似物吗?
【问题讨论】:
标签: c# .net-core remoting tcpchannel
由于 .NET Remoting 已从 .NET Core 框架中移除,我尝试使用 NetTcpBinding from the WCF library,但它未包含在 .NET Core 中。
我可以使用其他 TCPChannel 的类似物吗?
【问题讨论】:
标签: c# .net-core remoting tcpchannel
【讨论】:
如果您有一个基于 .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 项目的开发者。
【讨论】: