Monday, December 31, 2007 - Posts

MetaWeblog API library stuff part 2: Hints on implementing a BlogProvider (corrected typo in earlier version of title)

Technorati Tags: MetaWeblog API, WCF

Notice I said "hints", I don't want to raise anyone's expectations unduly :-)

One thing before I start: If you're working with the code from my previous post and construct a POX service using those guidelines, the url will be something like "(site name)/(application name)/blogservice.svc/metaweblog". This can probably be made friendlier on IIS7 but I haven't tried yet.

Anyway, here's  the simplest and most utterly useless implementation of IBlogProvider.
You'll notice that every method is a stub returning a NotImplementedException. That won't even allow you to register your test blog site with a blog client, although it will return an XML-RPC fault structure containing the error text, which is a test in itself.

To register your blog with a blog client application you'll need to have the GetUsersBlogs method actually return a BlogInfo struct (in the C#, not XML-RPC sense) populated with some convincing values.

To do some decent testing (and as a starting point for a real implementation) you can download this version: It doesn't actually read any real data or edit any real blog posts, but it does an adequate job of pretending. The exception is the NewMediaObject method, which still returns a NotImplementedException. Any implementation must return a url for the uploaded file, as mentioned in the code comments.
If you read that file you'll notice frequent calls to ValidateUser(), declared at the end of the class as a partial method (one of the nifty new features in .NET 3.5/VS 2008). If you include this optional implementation of the partial method, the user credentials supplied will be checked against the ASP.NET Membership Provider, and the user will additionally be checked to see whether they are in the role specified in the blogUserRoleName application setting (that is, in the  appSettings section in your config file). You can of course change the code to validate against anything you like.

FakeBlogProvider.cs hopefully contains enough comments to point you in the right direction to create a working implementation of IBlogProvider.
It goes without saying that all the code presented here and in the previous post is provided "as is" (well of course, how could it be provided "as it isn't" ?), with no assurances or guarantees as to its quality, accuracy, or fitness for any purpose whatsoever. And I can guarantee it will not improve your social life.
Anyway, if you find this useful I'd be interested to hear about it, so feel free to use the contact form and let me know.