General .NET (RSS)

A description would surely be a case of superfluous unhelpful commenting, just like "int counter; //Counter"

More BlogApi fixes....All done now I think

Anyone who's interested can get the latest version here.

I've been dogfooding this one with Windows Live Writer, and found and fixed a variety of bugs that were preventing the two from working together in peace and happiness etc.

I've tested adding a post, editing a post, including categories (which was one of the sources of trouble), retrieving recent posts, retrieving one of those posts for editing, and uploading images. I now have working implementations of all of these scenarios, and WLW now has no trouble with them.

So I'm reasonably confident in saying that after some embarrassing hiccups, niftiness has finally been achieved.

BlogApi fix

I just discovered a bug in my code for the MetaWeblog API getRecentPosts command - I was passing postId (which in this case is naturally empty) to the implementation method rather than blogId. So embarrassing.  Lucky nobody was actually using it :-)

The updated code can be found here.

I discovered this one since I'm writing an implementation for my own web site which I'm currently putting through its paces.
Other than that stupid, stupid little slip it's coming on quite well.

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.

MetaWeblog API library for .NET 3.5 (esp. WCF)

Technorati Tags: MetaWeblog API, .NET, WCF

[UPDATE 14/01/2008: A new version of the code is now available with numerous (OK, several) bug fixes, following testing in the real world]

The clock is about to tick over into 2008 here in Godzone, and all over the country happy drunks are merrily beating each other senseless. I can already hear local members of the knuckle-walking community cheerfully grunting to one another in what passes for a greeting among those of us whose ancestors spent insufficient time banging the rocks together...(I can think of no better way to start the new year than to insult 90% of the population. It'll save time later)

But I myself am sadly once more slaving over a hot keyboard. At least for a few more minutes (I have however marked the occasion by cracking open a Stella. But I digress). The reason I'm posting at this late hour is to make available the first version of a library I've written for Blog servers to work with the MetaWeblog API (as I've said before, I loathe XML-RPC, but you have to be pragmatic about these things). This means that you can enable your blog (if it doesn't have such support already) to work with tools such as Windows Live Writer. Which is nifty.

The idea is that you grab the aforementioned code from here, write a class implementing the IBlogProvider interface (I don't like using the -Provider name for something that isn't an ASP.NET Provider, but I couldn't think of anything better), and use said class to mediate between your data store (and if appropriate, file system) and the MetaWeblog-parsing-and-regurgitating code which is abstracted away into the MetaWeblogManager class. The library defines a few data structures such as BlogInfo, UserInfo, CategoryInfo and BlogEntry to enable complex data to be passed back and forth between MetaWeblogManager and its BlogProvider (the class you'll write to implement IBlogProvider) without the latter having to deal with or know about XML-RPC gunk.

Please note that this library requires .NET 3.5, as it relies heavily on LINQ to XML (and as you can see the following implementation bits use 3.5 as well).

It's designed to work with WCF 3.5 SOAP-less services, but could be made to work with something like an HttpHandler as well with a little extra code to convert between XElements and Request/Response stream content.

An example WCF contract looks like this:

using System.ServiceModel;
using System.ServiceModel.Web;
using System.Xml.Linq;

[ServiceContract]
public interface IBlogService
{

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml, Method = "POST")]
    XElement MetaWeblog(XElement request);
}

The implementing service being something like:

using System.Xml.Linq;
using Daly.Blogging;

public class BlogService : IBlogService
{
        MetaWeblogManager manager = new MetaWeblogManager { BlogProvider = new TestBlogProvider() };
        public XElement MetaWeblog(XElement request)
        {
            return manager.Execute(request);
        }
}

Note that TestBlogProvider is just an example - this would be whatever class you create to implement IBlogProvider.

I have to admit that for something so tiny separating the contract stuff out into a separate interface is a bit precious, so feel free not to do that. But that's just my opinion, I know some of you have Very Strong Views on that sort of thing.

The config for this service looks like this:

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="blogBinding" maxReceivedMessageSize="204800" ></binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="BlogService">
        <endpoint address="" behaviorConfiguration="Daly.Blogging.WebBehaviour" bindingConfiguration="blogBinding"
         binding="webHttpBinding" contract="IBlogService" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehaviour">
          <webHttp />
        </behavior>
        <behavior name="Daly.Blogging.WebBehaviour">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

This is actually only necessary in this case if you plan to support metaWeblog.newMediaObject and allow media files larger than about 20K (maxReceivedMessageSize has to be bumped up from its default 64K to allow for the base64-encoded-and-therefore-expanded file contents) - otherwise you can leave out the system.serviceModel config entries and include a Factory="System.ServiceModel.Activation.WebServiceHostFactory" attribute in your .svc file.

To answer a few questions you may have:

  1. What's with all this Daly.Blogging nonsense? - It's not, in fact, because I have an enormous ego (that's a coincidence). I've just used that namespace to avoid name collisions with your own code.
  2. Why use an interface instead of an abstract class for the "BlogProvider"? - Good question. I might change my mind about this later.
  3. Why are the (very few) fault code numbers defined as constants instead of an enum? - To avoid casting them back to System.Int32, and because they are never read, only written (er, that sort of made sense to me at the time).
  4. Why not implement a proper ASP.NET Provider?  - I planned to do this at one point (Sunday afternoon, to be precise), then decided that it didn't really fit the use case. The truth is that the provider will always be a custom implementation that you write yourself, there is no useful default or pre-packaged version. So there's not much point in providing declarative support. That being the case, I decided to keep it simple and avoid the Reflection overhead. For those who are interested in such things this implementation is more or less a Strategy pattern.

I may update the code over the next few days. I also intend to provide some useful information about implementing a BlogProvider, unless I forget. Or get hit by a meteorite. You know, stuff.

Anyway, Happy New Year.

Tip for NZ Daylight Saving changes

Those of us working in New Zealand will probably be aware (or should be aware) that our government in its wisdom decided on very short notice (on the principle that Anything I Don't Understand Isn't Important) to extend daylight saving from this year, basically to squeeze more money out of the tourists.

The updates for Windows are mostly available, but there is nevertheless room for uncertainty about whether any given machine has been patched.

A nice feature added to the DateTime structure in .NET 2.0 is the IsDaylightSavingTime method, which enables the following expression which can be run on any machine in the NZ timezone to test whether the daylight saving patch has been applied:

new DateTime(2007,9,30,3,0,0).IsDaylightSavingTime()

So there you go.

Visual Studio 2008 Beta 2 is on MSDN

Just thought you might like to know.

Productivity

Thought for the day: If I thought lines of code were truly a useful measure of productivity, I'd stop using the ternary operator.

But I won't.

Expression Web and Blend to be included in MSDN Premium subscription

Excellent news this morning from Somasegar.

Thanks to the Microsoft Expression blog for the heads up.

Now I just have to bite my nails for a while hoping Expression Blend is released before my MSDN subscription expires...

The WPF/E CTP is finally available

(and the site is up for real this time).

 

Go here for the details.

There's also an interview with Joe Stegman on Channel 9 here.

Now I'll step out of the way while the multitudes rush to make vitriolic if poorly-reasoned Flash comparisons.

 

Personally, I think this is cool (despite the JavaScript).

ASP.NET Hoster Evangelism Needed for .NET 3.0 (and no, the name doesn't help)

(Updated 9th August: corrected typo)

The time is almost upon us.
Whether or not Vista is delayed yet another month, the simple truth is that the new operating system and its associated goodies will soon be released.

So what has that got to do with ASP.NET hosting, since Longhorn Server (whatever its final name turns out to be) is at least another year away?

That's the obvious question, but it's a completely mistaken one. While we do indeed have a long time left to wait for the new server OS, that does not apply to most of the features that are of interest to developers. Since .NET 3.0 (formerly known as WinFX) will be made available for Windows XP and Windows Server 2003, the main feature of interest to developers that won't be available (as far as I know) on the server in the intervening period is IIS 7.0 (admittedly that's a biggie, especially since it will be sitting on our Vista workstations taunting us with its niftiness). Crucially, both Workflow and WCF will be available for installation on W2K3, and both of those are of interest to web developers.And let's not forget the wonders of System.IO.Packaging: there are (or should be) obvious benefits in being able to programmatically generate Office 2007 documents from public servers.

The point of this post is that it would be very helpful if Microsoft conducts an evangelism effort with ASP.NET 2.0 hosters to encourage them to install the new bits.

Windows Communication Foundation (WCF) is an obvious driver here: we want it, and we want it by last year. While it will be all very nice to get the new bits installed on corporate servers, let's not forget that the business cases for WSE are doubly valid for WCF, which has the advantage of being less painful to use and suffering less obvious tag incontinence (as well as doing more). WCF will live up to its potential when it moves beyound the corporate firewall, which is where the hosters come in (since after all, we don't all have a server in the basement...).

The .NET 3.0 name is unfortunate from the point of view of hoster adoption, since it could give rise to the mistaken assumption of a new version of ASP.NET with all the migration and testing issues that would entail. .NET 3.0 will only get installed if it can be made plain that it is an addition to .NET 2.0, not a replacement, and that there are no issues at all relating to ASP.NET and the versioning thereof. If that isn't made crystal clear, we might as well all start waiting for Longhorn Server.

But personally I choose to be optimistic. I'm just getting my nagging in early.

WinFS Morphs into The Late WinFS

News from Quentin Clark.

I was quite keen on the idea of a relational file store.
Oh well.

The status of client script in internet applications

With all the buzz (and to some extent, let's be honest, hype) apart the newly sexy and relabelled AJAX, I'm beginning to wonder about the continuing applicability of some of the design principles I have applied to web applications for the past 6 years.
When contrasting the new "Javascript sexy" world with the previous "Javascript crappy" one, many people speak as if the only reason we shied away from script-heavy web applications was that they were to say the least difficult to debug. That is of course not the whole story - it's easy to forget the browser story a few years ago was much worse than it is now, with IE and Netscape offering two completely different DHTML models and other browsers going their own strange way. So things are much better now if for no other reason than that everybody understands "getElementById" (one of the last reasons for browser sniffing involves AJAX and will be greatly mitigated by IE7, but that's another story)
But there was yet another issue: aside from those browsers that didn't have support for any version of Javascript let alone a DOM (I recall at one point using the not entirely helpful argument that their users probably didn't have any money anyway), towards the turn of the century it became common practice for organisations to block or disable Javascript for security reasons.
That led to the logical conclusion: if you want to expose your site or application broadly on the public internet, you can't assume that Javascript will be available.
Hence the rule: No mission-critical client script. The ASP.NET validators exemplify this approach: by default they will be triggered on the client, but the server-side validation is always triggered in any case, so that even if Javascript is blocked the control still works (or if it doesn't, it's for a different reason).
And that's the way we've approached things in an online world where client script may or may not be enabled: we've had to treat it as something that improves the user experience where it's available, but without which the application still functions. 

AJAX-enabled applications could be designed the same way, but I can confidently predict that the non-AJAX user experience will be much more painful than if the UI were designed with full page refreshes in mind, since the design choices that would normally be made to mitigate the limitations of that approach are likely not to have been made, or may simply not be practical without providing two different versions of the site. In other words, everything that is unpleasant and cumbersome about a non-AJAX web application is likely to be even more so for AJAX applications being used er, without AJAX.
To give a roughly analogous example: you may recall when framesets were in vogue (before they were recognised as evil), we had the element to contain markup to display typically for browsers that did not support framesets or had them disabled. Now, let's be honest: how often was this feature used in a way much more helpful than the following:

It appears that your browser does not support frames, or has them disabled. Shame.

Sooooooooooooooo...I'd be very interested to hear what people think: have we finally moved on to a stage where it can safely be assumed for most purposes that client script will be enabled?

Development implications of UMPC

So, the hype has come and gone,and now we are temporarily left with nothing but the whining of people complaining that a UMPC makes a crappy iPod...

As I mentioned previously in my recent Geekzone post, Alex Yakhnin has astutely identified the reasons why Windows Mobile/Compact Framework developers are well-placed to take advantage of the UMPC platform (or form factor, or whatever we choose to call it). So anyway, I didn't set out today to indulge in a self-referential link-fest, so it's about time I said something new.

The point I want to make today is that the arrival (sort of) of UMPC devices underlines in big red, um, underlines something that Microsofties have been telling us for a while: it's time to make ourselves resolution-independent. This doevetails quite well with the message that accompanies Avalon  WPF loveliness: we have to ensuure that our content will look good at any size, at any resolution. We can no longer afford to regard the display area as a fixed surface upon which we display visual elements as we see fit. From now on the Windows developer must borrow habits of thought from the web developer, and assume the same flexibility. Our content will be displayed on