XmlRpcClientProxy.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. namespace Nwc.XmlRpc
  2. {
  3. using System;
  4. using System.Runtime.Remoting.Proxies;
  5. using System.Runtime.Remoting.Messaging;
  6. /// <summary>This class provides support for creating local proxies of XML-RPC remote objects</summary>
  7. /// <remarks>
  8. /// To create a local proxy you need to create a local C# interface and then, via <i>createProxy</i>
  9. /// associate that interface with a remote object at a given URL.
  10. /// </remarks>
  11. public class XmlRpcClientProxy : RealProxy
  12. {
  13. private String _remoteObjectName;
  14. private String _url;
  15. private XmlRpcRequest _client = new XmlRpcRequest();
  16. /// <summary>Factory method to create proxies.</summary>
  17. /// <remarks>
  18. /// To create a local proxy you need to create a local C# interface with methods that mirror those of the server object.
  19. /// Next, pass that interface into <c>createProxy</c> along with the object name and URL of the remote object and
  20. /// cast the resulting object to the specifice interface.
  21. /// </remarks>
  22. /// <param name="remoteObjectName"><c>String</c> The name of the remote object.</param>
  23. /// <param name="url"><c>String</c> The URL of the remote object.</param>
  24. /// <param name="anInterface"><c>Type</c> The typeof() of a C# interface.</param>
  25. /// <returns><c>Object</c> A proxy for your specified interface. Cast to appropriate type.</returns>
  26. public static Object createProxy(String remoteObjectName, String url, Type anInterface)
  27. {
  28. return new XmlRpcClientProxy(remoteObjectName, url, anInterface).GetTransparentProxy();
  29. }
  30. private XmlRpcClientProxy(String remoteObjectName, String url, Type t) : base(t)
  31. {
  32. _remoteObjectName = remoteObjectName;
  33. _url = url;
  34. }
  35. /// <summary>The local method dispatcher - do not invoke.</summary>
  36. override public IMessage Invoke(IMessage msg)
  37. {
  38. IMethodCallMessage methodMessage = (IMethodCallMessage)msg;
  39. _client.MethodName = _remoteObjectName + "." + methodMessage.MethodName;
  40. _client.Params.Clear();
  41. foreach (Object o in methodMessage.Args)
  42. _client.Params.Add(o);
  43. try
  44. {
  45. Object ret = _client.Invoke(_url);
  46. return new ReturnMessage(ret,null,0,
  47. methodMessage.LogicalCallContext, methodMessage);
  48. }
  49. catch (Exception e)
  50. {
  51. return new ReturnMessage(e, methodMessage);
  52. }
  53. }
  54. }
  55. }