尝试使用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;
}
}
我的客户端无法连接到我的服务器,它看不到那里的方法。错误是:“操作不存在”。
有人有任何线索吗?
谢谢!!