Windows Azure, Microsoft .NET Services – Working with Routers (II)

[This post is based on the Windows Azure Tools for Microsoft Visual Studio July 2009 CTP]

In the previous post on Working with Routers I showed how you can create a router using a Http Post.  A router is only really useful when it can actually route messages. To do this it needs one or more subscribers.  As you can imagine creating a subscriber (as with other creates) involves a Http Post.  When we created the router one of the links that was returned was the subscriptions url. All you really need to do to create a subscriber is send a Http Post to this url. Of course you need to send the url where you want messages to be routed to, and any other information that is required for the router to route messages.  The upshot is that you end up sending an entry that looks similar to the following:

<entry ot;;

    XElement content = new XElement(XName.Get("entry", "http://www.w3.org/2005/Atom"),
                            new XElement(XName.Get("link", "
http://www.w3.org/2005/Atom"),
                                new XAttribute("rel", "alternate"),
                                new XAttribute("href", "
https://blogsample.servicebus.windows.net/" + queue)),
                            ServicesElement("HttpHeaders",
                                ServicesElement("HttpHeader",
                                    new XAttribute("name","X-MS-Identity-Token"),
                                    new XAttribute("value",token))));

    using (var requestStream = request.GetRequestStream())
    using (var writer = new System.IO.StreamWriter(requestStream))
    {
        writer.Write(content);
        writer.Flush();
    }

    using (var response = request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var reader = new StreamReader(responseStream))
    {
        return reader.ReadToEnd();
    }
}

In this example the link url provided is the tail of the queue (ie the url where you enqueue messages).  The token is included in a HttpHeader element so that it can be routed as part of sending the message to the queue. You should expect a 200, Ok response. If you want to get a list of subscribers you can simply issue a Http Get to the subscriptions url.  This will return a feed of the subscribers.

<feed ot;;

    using (var requestStream = request.GetRequestStream())
    using (var writer = new System.IO.StreamWriter(requestStream))
    {
        writer.Write(message);
        writer.Flush();
    }

    using (var response = request.GetResponse())
    {
        return (response as HttpWebResponse).StatusCode.ToString();
    }
}

Again you should expect a 202, Accepted response. To retrieve the message off the destination queue, simply send a Http Delete message as discussed in Working with Queues (III).

Leave a comment