MessagePack RPC C# - 服务器端
MessagePack RPC C# - Server side
我在尝试使用 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;
}
}
我的客户端无法连接到我的服务器,它看不到那边的方法。错误是:"operation does not exist".
有人知道吗?
谢谢!!
经过多次尝试和朋友的帮助,我终于设法解决了这个问题。
首先,您必须下载 Message Pack RPC 解决方案 here 并在此解决方案中创建新项目(服务器或客户端)。在我的例子中,当我创建一个新的解决方案并引用 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 ).
我在尝试使用 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;
}
}
我的客户端无法连接到我的服务器,它看不到那边的方法。错误是:"operation does not exist".
有人知道吗?
谢谢!!
经过多次尝试和朋友的帮助,我终于设法解决了这个问题。 首先,您必须下载 Message Pack RPC 解决方案 here 并在此解决方案中创建新项目(服务器或客户端)。在我的例子中,当我创建一个新的解决方案并引用 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 ).