C#利用XML-RPC实现进程间通讯
本帖最后由 MyNameIsLiLei 于 2020-10-22 11:25 编辑XML-RPC简单易用,可以轻松实现跨语言跨进程通讯,这里先用C#来实现。
服务端代码:
1.启动RPC服务
protected void InitServer()
{
IDictionary props = new Hashtable();
//XmlRpcClientFormatterSinkProvider
props["name"] = "Rem";
props["port"] = 8088;
channel = new HttpChannel(props, null, new XmlRpcServerFormatterSinkProvider());
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server), "RPC2", WellKnownObjectMode.Singleton);
}
2.定义要被远程调用的方法接口public interface IServer
{
string SayHello();
}3.创建类实现2中的接口
public class Server : MarshalByRefObject, IServer
{
public string SayHello()
{
return "Hello";
}
}
客户端代码:
1.定义要被远程调用的方法接口
public interface IRPCMethod
{
string SayHello();
}
2.调用
private static void Main(string[] args)
{
var chnl = new HttpChannel(null, new XmlRpcClientFormatterSinkProvider(), null);
ChannelServices.RegisterChannel(chnl, false);
svr = (IRPCMethod)Activator.GetObject(typeof(IRPCMethod), "http://localhost:8088/RPC2");
Console.WriteLine("成功注册信道");
string str = svr.SayHello();
Console.WriteLine(str);
}
需要添加引用System.Runtime.Remoting 还在学习C#中,还没到进程部分呢,关注一下
页:
[1]