Specify XML-RPC Endpoints at Run Time With CookComputing XMLRPC.net

S

A common question when using Cook Computing XML RPC.net to talk to blogs etc… is how to specify a blog / endpoint at runtime? Most of the examples seem to specify the details in an attribute… not much use if you’re trying to develop a wrapper.

To enable me to create JoeBlogs, I made use of the IXmlRpcProxy Proxy classes…

I created my interface, implementing IXmlRpcProxy:

We define the method as an XML RPC method using XmlRpcMethod.
The name of the method is also specified.
In this case, we are using getPost from the metaWeblog XML RPC API – WordPress supports this, as well as the Blogger API and the Movable Type API.
They also have their own (which is an extension to the Moveable Type API) –
http://codex.wordpress.org/XML-RPC_wp

Anyway…..

As per the metaWeblog.getPost specification, we need to pass in the postid, username, and password. So we create our interface as follows:

	public interface IMyProxy: IXmlRpcProxy
	{
		[XmlRpcMethod("metaWeblog.getPost")]
		Post GetPost(string postid, string username, string password);
  	}

As we can see, this returns a “Post”

This is a struct, that basically defines the structure of the response.

	[XmlRpcMissingMapping(MappingAction.Ignore)]
	public struct Post
	{
		public DateTime dateCreated;
		public string description;
		public string title;
		public string postid;
		public string[] categories;

		public override string ToString()
		{
			return this.description;
		}
	}

To use this,  you need to create an instance of the proxy object.

XML RPC.net provides a method called XmlRpcProxyGen to do this for us.

We then set the Url, which is declared as part of IXmlRpcProxy (remember IMyProxy implemented this interface…)

Since our proxy declared the method GetPost, we can now use this, pass in the required post id, username and password.

    public void myTest()
    {
      string postId = "1234";
      string username = "myUsername";
      string password = "password";

      var proxy = (IMyProxy)XmlRpcProxyGen.Create(typeof(IMyProxy));
      proxy.Url = "www.alexjamesbrown.com";

      var post = proxy.GetPost(postId, username, password);
    }

And there we have it.

Of course this is a very bare bones example.

JoeBlogs contains much more separation of these concerns.