0

尝试使用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;
    }

}

我的客户端无法连接到我的服务器,它看不到那里的方法。错误是:“操作不存在”。

有人有任何线索吗?

谢谢!!

4

1 回答 1

1

经过多次尝试和朋友的帮助,我终于设法解决了这个问题。首先,您必须在此处下载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 });
    }

我希望这很清楚,并帮助你们解决它。不要忘记在服务器上将方法设置为公开。感谢https://github.com/yfakariya /msgpack-rpc-cli/issues/6回答了我的问题。真的特别感谢@DanielGroh,他花了一些时间和我一起尝试修复它,还有 Gilmar Pereira,这里没有个人资料,但他是一位出色的开发人员并帮助了我很多(https://www.facebook.com/gilmarps?fref=ts )。

于 2016-01-25T11:01:03.200 回答