客户端
using CookComputing.XmlRpc;
using System;
namespace BM.Leader.Net
{
public struct StateStructRequest
{
public int state1;
public int state2;
public int state3;
}
[XmlRpcUrl("http://localhost:7762/StateNameServer.ashx")]
public interface IStateName : IXmlRpcProxy
{
[XmlRpcMethod("examples.getStateName")]
string GetStateName(int stateNumber);
[XmlRpcBegin("examples.getStateName")]
IAsyncResult BeginGetStateName(int stateNumber);
[XmlRpcEnd]
string EndGetStateName(IAsyncResult asr);
[XmlRpcMethod("examples.getStateStruct")]
string GetStateNames(StateStructRequest request);
}
public class RPCHelper
{
public void TestRPC()
{
try
{
IStateName proxy = XmlRpcProxyGen.Create<IStateName>();
Console.WriteLine("Synchronous call:");
string ret = proxy.GetStateName(45);
Console.WriteLine("state #45 is {0}", ret);
Console.WriteLine("Asynchronous call:");
IAsyncResult asr = proxy.BeginGetStateName(46);
asr.AsyncWaitHandle.WaitOne();
string aret = proxy.EndGetStateName(asr);
Console.WriteLine("state #46 is {0}", aret);
}
catch (XmlRpcFaultException fex)
{
Console.WriteLine(fex.FaultString);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
服务端
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using CookComputing.XmlRpc;
public class StateNameServer : MarshalByRefObject, IStateName
{
public string GetStateName(int stateNumber)
{
if (stateNumber < 1 || stateNumber > m_stateNames.Length)
throw new XmlRpcFaultException(1, "Invalid state number");
return m_stateNames[stateNumber-1];
}
public string GetStateNames(StateStructRequest request)
{
if (request.state1 < 1 || request.state1 > m_stateNames.Length)
throw new XmlRpcFaultException(1, "State number 1 invalid");
if (request.state2 < 1 || request.state2 > m_stateNames.Length)
throw new XmlRpcFaultException(1, "State number 1 invalid");
if (request.state3 < 1 || request.state3 > m_stateNames.Length)
throw new XmlRpcFaultException(1, "State number 1 invalid");
string ret = m_stateNames[request.state1-1] + " "
+ m_stateNames[request.state2-1] + " "
+ m_stateNames[request.state3-1];
return ret;
}
string[] m_stateNames = { "Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Lousiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississipi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylviania", "Rhose Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming" };
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Server StartUp !");
IDictionary props = new Hashtable();
props["name"] = "localhost";
props["port"] = 7762;
HttpChannel channel = new HttpChannel(
props,
null,
new XmlRpcServerFormatterSinkProvider()
);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(StateNameServer),
"StateNameServer.ashx",
WellKnownObjectMode.Singleton);
Console.WriteLine("Press <ENTER> to shutdown");
Console.ReadLine();
}
}
}
可参考资源
https://download.csdn.net/download/qq_39024280/33445713
|