Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

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 xmlns="http://www.w3.org/2005/Atom">
  <link rel="alternate" href="
https://blogsample.servicebus.windows.net/myqueue" />
  <HttpHeaders xmlns="
http://schemas.microsoft.com/netservices/2009/05/servicebus/connect">
    <HttpHeader name="X-MS-Identity-Token" value="zpleGHYQzEiRUXkdFd7UN+gBbBzSIuW5Bc2hIA==" />
  </HttpHeaders>
</entry>

Of course, this needs to be sent as a Http Post:

private static string SubscribeQueueToRouter(string token, string router, string queue)
{
    string subscriptionsUri = "
https://blogsample.servicebus.windows.net/" + router + "/!(router/subscriptions)";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(subscriptionsUri);
    request.Headers.Add("X-MS-Identity-Token", token);
    request.Method = "POST";
    request.ContentType = "application/atom+xml;type=entry;charset=utf-8";

    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 xmlns="http://www.w3.org/2005/Atom">
   <title type="text">Subscriptions</title>
   <id>uuid:eb256797-2832-4f2b-afd9-ba3e4cfeeb7b;id=1869</id>
   <updated>2009-09-01T06:20:00Z</updated>
   <link rel="self" href="https://blogsample.servicebus.windows.net/myrouter/!(router/subscriptions)"></link>
   <entry>
     <id>uuid:eb256797-2832-4f2b-afd9-ba3e4cfeeb7b;id=1870</id>
      <title type="text">urn:uuid:90d62d9e-2a13-446c-951a-72046101fa17</title>
      <updated>2009-09-01T06:20:00Z</updated>
      <link rel="alternate" href="https://blogsample.servicebus.windows.net/myqueue"></link>
      <link rel="self" href="https://blogsample.servicebus.windows.net/myrouter/!(router/subscriptions/urn:uuid:90d62d9e-2a13-446c-951a-72046101fa17)"></link>
      <content type="text"></content>
      <Expires xmlns="http://schemas.microsoft.com/netservices/2009/05/servicebus/connect">2009-09-02T06:18:56.9899631Z</Expires>
      <NotifyTo xmlns="http://schemas.xmlsoap.org/ws/2004/08/eventing">
         <Address xmlns="http://www.w3.org/2005/08/addressing">https://blogsample.servicebus.windows.net/myqueue</Address>
      </NotifyTo>
      <HttpHeaders xmlns="http://schemas.microsoft.com/netservices/2009/05/servicebus/connect">
         <HttpHeader name="X-MS-Identity-Token" value="zpleGHYQzEiRUXkdFd7UN+gBbBzSIuW5Bc2hIA=="></HttpHeader>
      </HttpHeaders>
   </entry>
   <entry>
      <id>uuid:eb256797-2832-4f2b-afd9-ba3e4cfeeb7b;id=1871</id>
      <title type="text">urn:uuid:d27a29ab-46e4-49ca-9d1e-cd05bdc24d42</title>
      <updated>2009-09-01T06:20:00Z</updated>
      <link rel="alternate" href="https://blogsample.servicebus.windows.net/myqueue"></link>
      <link rel="self" href="https://blogsample.servicebus.windows.net/myrouter/!(router/subscriptions/urn:uuid:d27a29ab-46e4-49ca-9d1e-cd05bdc24d42)"></link>
      <content type="text"></content>
      <Expires xmlns="http://schemas.microsoft.com/netservices/2009/05/servicebus/connect">2009-09-02T06:01:01.0635156Z</Expires>
      <NotifyTo xmlns="http://schemas.xmlsoap.org/ws/2004/08/eventing">
         <Address xmlns="http://www.w3.org/2005/08/addressing">https://blogsample.servicebus.windows.net/myqueue</Address>
      </NotifyTo>
      <HttpHeaders xmlns="http://schemas.microsoft.com/netservices/2009/05/servicebus/connect">
         <HttpHeader name="X-MS-Identity-Token" value="J+rsqHMQzEgxQ3yu3JwyDv3J8wNgIQybBoxtng=="></HttpHeader>
      </HttpHeaders>
   </entry>
</feed>

Now the last thing to do is to send a message to the router. In the above example a message sent to the router will get routed through to the subscribed queue. As we did with enqueuing a message on a queue, to send a message to the router you do a Http Post to the alterate url of the router (this is also the original url that you used to create the router).

private static string SendMessageToRouter(string token, string router, string message)
{
    string routerUri = "
https://blogsample.servicebus.windows.net/" + router;
    // send
    HttpWebRequest request = HttpWebRequest.Create(routerUri) as HttpWebRequest;
    request.Method = "POST";
    request.Headers.Add("X-MS-Identity-Token", token);
    request.ContentType = "text/plain;charset=utf-8";

    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).

Comments are closed