GeeksWithBlogs.net runs on the .Text ASP.NET weblog. .Text provides a couple of services to allow you to program against your blog: The SimpleBlogService and
the MetablogAPI. SimpleBlogService is a .net web service, and MetablogAPI is an XML-RPC service. For the application I'm working on, I need the functionality in the latter. Chuck Cook has written a nice library for using XML-RPC in .NET projects. So I've been taking a look at that.
There are three ways to implement an XML-RPC client with this library:
- Define an interface which defines the server's methods, and use XmlRpcProxyGen to generate the proxy class.
- Manually write a class to invoke methods on the server (using the XML-RPC.NET library, easier than it sounds).
- Or, use Joe Bork's XmlRpcProxyCodeGen class to generate code for the proxy class. I went with this method, because it sounds cool to be able to generate code programatically (not from string building...)
So, how do you do it? It's pretty easy. The XML-RPC.NET code includes a sample MetablogAPI interface. Taking a look at it, it's pretty simple (but nice that it's already written). Here's just the method for getRecentPosts:
[XmlRpcMethod("metaWeblog.getRecentPosts",
Description="Retrieves a list of the most recent existing post "
+ "using the metaWeblog API. Returns the metaWeblog struct collection.")]
Post[] getRecentPosts(
string blogid,
string username,
string password,
int numberOfPosts);
You give that interface to the XmlRpcProxyCodeGen class:
// Create a CSharpCodeProvider, since I'd like code in C#
Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
// Ask it for the code gen interface
System.CodeDom.Compiler.ICodeGenerator codeGen = codeProvider.CreateGenerator();
// Setup some options, with a namespace and class name
Headblender.XmlRpc.XmlRpcProxyCodeGenOptions options = new
Headblender.XmlRpc.XmlRpcProxyCodeGenOptions("Community.BlogUtils.BlogApi", "MetaWeblog", false, false);
// Generate the code for the IMetaWeblog interface
string strCode = Headblender.XmlRpc.XmlRpcProxyCodeGen.CreateCode( typeof(IMetaWeblog), codeGen, options );
// Write out the code to a file. Done!
StreamWriter tw = new StreamWriter("MetaWeblog.cs", false);
tw.Write( strCode );
tw.Close();
And it spits out some code (here's a snippet):
[CookComputing.XmlRpc.XmlRpcMethodAttribute("metaWeblog.getRecentPosts")]
public CookComputing.MetaWeblog.Post[] getRecentPosts(
string blogid, string username, string password, int numberOfPosts)
{
object xrtTemp = null;
CookComputing.MetaWeblog.Post[] xrtReturn = null;
object[] xrtArray = new object[] {
blogid,
username,
password,
numberOfPosts};
xrtTemp = this.Invoke("getRecentPosts", xrtArray);
xrtReturn = ((CookComputing.MetaWeblog.Post[])(xrtTemp));
return xrtReturn;
}
I've got my proxy class, now on to the rest of the app...
BTW, you can browse these service endpoints for my blog here:
MetablogAPI and
SimpleBlogService.