MessagePack RPC C#-服务器端
本文关键字:服务器端 C#- RPC MessagePack | 更新日期: 2023-09-27 18:29:55
在尝试使用MessagePack RPC
的服务器实现时遇到问题。我根据公司客户提供的Python代码为客户端和服务器编写了一个实现。
服务器实现应该由Python使用,但据我所见,这不会是一个问题。
服务器实现:
public class Program
{
static void Main(string[] args)
{
try
{
DefaultServiceTypeLocator def = new DefaultServiceTypeLocator();
ServiceTypeLocator ser = def;
def.AddService(new Methods().GetType());
var services = ser.FindServices();
var configuration = new RpcServerConfiguration();
IPAddress ipAddress = GetIp();
configuration.BindingEndPoint = new IPEndPoint(ipAddress, 8089);
Console.WriteLine(new IPEndPoint(ipAddress, 8089).ToString());
using (var server = new RpcServer(configuration))
{
server.Start();
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.Write(ex);
Console.ReadKey();
}
}
[MessagePackRpcServiceContractAttribute]
public class Methods
{
[MessagePackRpcMethodAttribute]
public int hello0()
{
Console.WriteLine("hello0");
return 0;
}
}
客户端实现:
public class Program
{
static void Main(string[] args)
{
try
{
var configuration = new RpcClientConfiguration();
IPAddress ipAddress = GetIp();
using (dynamic proxy = new DynamicRpcProxy(new IPEndPoint(ipAddress, 8089), configuration))
{
dynamic res = proxy.hello0();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey();
}
}
private static IPAddress GetIp()
{
string myHost = System.Net.Dns.GetHostName();
IPAddress myIP = null;
for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
{
if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
{
if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i];
}
}
return myIP;
}
}
我的客户端无法连接到我的服务器,它看不到那里的方法。错误为:"操作不存在"。
有人知道吗?
谢谢!!
经过多次尝试和朋友的帮助,我终于解决了这个问题。首先,您必须在此处下载Message Pack RPC解决方案,并在此解决方案中创建新项目(服务器或客户端)。在我的案例中,当我创建一个新的解决方案并引用dll时,服务器没有工作,只是在整个项目中。客户端仅引用dll进行工作。
服务器端的实现:
internal class Program
{
public static void Main(string[] args1)
{
var config = new RpcServerConfiguration();
config.BindingEndPoint = new IPEndPoint(IPAddress.Loopback, 8089);
config.PreferIPv4 = true;
config.IsDebugMode = true;
//UseFullMethodName is a property that if it is false allows you in the CLIENT to call the methods only by it's name, check example further.
config.UseFullMethodName = false;
var defaultServiceTypeLocator = new DefaultServiceTypeLocator();
//Methods is the class I created with all the methods to be called.
defaultServiceTypeLocator.AddService(typeof(Methods));
config.ServiceTypeLocatorProvider = conf => defaultServiceTypeLocator;
using (var server = new RpcServer(config))
{
server.Start();
Console.ReadKey();
}
}
[MessagePackRpcServiceContract] //Define the contract to be used
public class Methods
{
[MessagePackRpcMethod] //Define the methods that are going to be exposed
public string Hello()
{
return "Hello";
}
[MessagePackRpcMethod]
public string HelloParam(string i)
{
return "Hello " + i;
}
}
客户端的实现:
static void Main(string[] args)
{
using (var target = CreateClient())
{
//var result1 = target.Call("Hello:Methods:0", null); /*if in the server the property UseFullMethodName is true, or not defined as false (default is true) you have to call the method on the server using *methodname:class:version**/
var result2 = target.Call("Hello", null); //Parameter is null
var result3 = target.Call("HelloParam", “Mariane”);//Method with parameter
}
}
public static RpcClient CreateClient()
{
return new RpcClient(new IPEndPoint(IPAddress.Loopback, 8089), new RpcClientConfiguration() { PreferIPv4 = true });
}
我希望这是清楚的,并帮助你们解决它。不要忘记在服务器上设置方法为public。幸亏https://github.com/yfakariya/msgpack-rpc-cli/issues/6,它回答了我的问题。真的很特别,感谢@DanielGroh花了一些时间和我一起尝试修复它,感谢Gilmar Pereira,他在这里没有个人资料,但是一个伟大的开发人员,帮了我很多忙(https://www.facebook.com/gilmarps?fref=ts)。