嘗試使用服務器實現MessagePack RPC
時遇到問題。我根據我公司客戶提供的Python代碼爲客戶端和服務器編寫了一個實現。MessagePack RPC C# - 服務器端
服務器實現應該由Python使用,但據我所知,這不會是一個問題。
Server實現:
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;
}
}
我的客戶端無法連接到我的服務器,它不能看到那邊的方法。錯誤是:「操作不存在」。
任何人有任何線索?
謝謝!
您必須使用'DefaultServiceTypeLocator'設置ServiceTypeLocator。使用'.AddService()'添加你想暴露的類' – Caramiriel
Hi @Caramiriel,謝謝你的評論!我做了你所說的(我認爲),我也添加了方法和類屬性,但仍然不能這樣做=( –
@MarianePinheiro請在對代碼進行更改後提供更新,以便我們可以得知究竟是什麼已被更改 – DAG