123 posts categorized "Interoperability"

12/20/2011

Cross-device/cross-location Pub-Sub (part 3): Using Windows Azure Service Bus Topics & Subscriptions in Android with Mono for Android

And here we go with the final piece of blogging around the Windows Azure Service Bus and its cross-device, cross-location pub/sub features.

There have already been two articles about this topic (pun intended Winking smile)


Today I have built a very simple Android app with MonoDroid (aka Mono for Android) in C#. The code is essentially the same as for the iOS demo shown earlier (with MonoTouch), I just used Monodroid.Dialog to programmatically wire up the UI.

image

This is the entire code for the app:

   1:  [Activity(Label = "DroidServiceBusSubscriber",
   2:    MainLauncher = true, Icon = "@drawable/icon")]
   3:  public class MainActivity : Activity
   4:  {
   5:      private ListView lv;
   6:      private DialogAdapter da;
   7:      private RootElement menu;
   8:      private Section messages;
   9:      private BrokeredMessaging broker;
  10:      private string sbNamespace = "YOUR_SB_NS";
  11:      private string issuerName = "owner";
  12:      private string issuerSecret = "YOUR_OWNER_ISSUERKEY";
  13:      private string topicName = "newstopic";
  14:      private string subscriptionName = "androidsubscription";
  15:   
  16:      protected override void OnCreate(Bundle bundle)
  17:      {
  18:          base.OnCreate(bundle);
  19:   
  20:          InitBroker();
  21:          SetupUI();
  22:          RegisterMessagesHandler();
  23:      }
  24:   
  25:      private void InitBroker()
  26:      {
  27:          broker = new BrokeredMessaging(sbNamespace);
  28:          broker.GetToken(issuerName, issuerSecret);
  29:      }
  30:   
  31:      private void SetupUI()
  32:      {
  33:          messages = new Section("Messages");
  34:          menu = new RootElement("Service Bus Subscriber")
  35:          {
  36:              messages
  37:          };
  38:   
  39:          da = new DialogAdapter(this, menu);
  40:          lv = new ListView(this) { Adapter = da };
  41:              
  42:          SetContentView(lv);            
  43:      }
  44:   
  45:      private void RegisterMessagesHandler()
  46:      {
  47:          Task.Factory.StartNew(() =>
  48:          {
  49:              while (true)
  50:              {
  51:                  try
  52:                  {
  53:   
  54:                      var message = broker.ReceiveAndDeleteMessage(topicName + 
  55:                        "/Subscriptions/" + subscriptionName);
  56:   
  57:                      if (!String.IsNullOrWhiteSpace(message))
  58:                      {
  59:                          RunOnUiThread(delegate
  60:                          {
  61:                              messages.Elements.Add(new StringElement(message));
  62:                              da.NotifyDataSetChanged();
  63:                          });
  64:                      }
  65:   
  66:                  }
  67:                  catch (Exception ex)
  68:                  {
  69:                      Console.WriteLine(ex);
  70:                  }
  71:              }
  72:          });
  73:      }
  74:  }

 

And the ServiceBusBrokeredMessaging class is exactly copied and pasted from the MonoTouch project to the VS 2010 project for MonoDroid.

Here is the client app solution (you will need Mono for Android for Visual Studio):


Hope this helps.

12/19/2011

Hosting a public web site (ASP.NET) and an internal services site (WCF) in one Windows Azure web role – sample code

Alright. I got some emails asking for the actual project and code for the blog post over here.

Imagine you want to host some (Web) services in an IIS web site inside your Windows Azure compute web role. In addition, you also want to host your web application/portal in another web site in Full IIS in the *same* web role. […]

Here is a working solution (based on Windows Azure Tools for VS 2010 1.6).
Hope this helps.

12/15/2011

Cross-device/cross-location Pub-Sub (part 2): Using Windows Azure Service Bus Topics & Subscriptions in Windows Phone (7.1)

In the previous post I showed how to use MonoTouch and C# on iOS devices to subscribe to the Windows Azure Service Bus’s topics and subscriptions features.

This time it is a Windows Phone (Mango) client app.

The sending / publishing application is still the same Console application from the last post:

image

And the nice part about the WP app is that Kellabyte already did the major work for wrapping the Service Bus queues, topics, and subscriptions REST API (also check out her awesome Continuous Client demo which uses the Service Bus). Smile

With her code in place the actual subscribing inside my WP app to the messages in my Service Bus subscriptions is a piece of cake:

   1:  public MainPage()
   2:  {
   3:      InitializeComponent();
   4:   
   5:      InitBroker();
   6:  }
   7:   
   8:  private void InitBroker()
   9:  {
  10:      token = new Token(sbNamespace, issuerName, issuerSecret);
  11:      token.Requested += new EventHandler<TokenEventArgs>(token_Requested);
  12:      token.Request();
  13:  }
  14:   
  15:  private void token_Requested(object sender, TokenEventArgs e)
  16:  {
  17:      topic = new Topic(token, topicName);
  18:   
  19:      RegisterMessagesHandler();
  20:  }
  21:   
  22:  private void RegisterMessagesHandler()
  23:  {
  24:      subscription = new Subscription(topic, subscriptionName);
  25:      subscription.MessageRecieved += 
  26:                    new EventHandler<SubscriptionMessageEventArgs>(
  27:                      subscription_MessageRecieved);
  28:      subscription.ReceiveMessage();
  29:  }
  30:   
  31:  private void subscription_MessageRecieved(object sender, SubscriptionMessageEventArgs e)
  32:  {
  33:      var message = e.Message;
  34:   
  35:      try
  36:      {
  37:          if (!String.IsNullOrWhiteSpace(message))
  38:          {
  39:              Dispatcher.BeginInvoke(() =>
  40:              {
  41:                  mainContentPanel.RowDefinitions.Add(
  42:                      new RowDefinition { Height = new GridLength(35) });
  43:   
  44:                  var pos = mainContentPanel.RowDefinitions.Count - 1;
  45:   
  46:                  var txt = new TextBlock();
  47:                  txt.Text = message;
  48:                  mainContentPanel.Children.Add(txt);
  49:                  Grid.SetColumn(txt, 0);
  50:                  Grid.SetRow(txt, pos);
  51:              });
  52:          }
  53:      }
  54:      catch (Exception ex)
  55:      {
  56:          Console.WriteLine(ex);
  57:      }
  58:   
  59:      subscription.ReceiveMessage();
  60:  }


And the (again) super spectacular result is that all the messages are being sucked from my subscription and displayed on the phone:

image

Voila. Sorry – not so exciting as last time. Anyway, here is the download:


Hope this helps.

12/14/2011

Cross-device/cross-location Pub-Sub: Using Windows Azure Service Bus Topics & Subscriptions in iOS with MonoTouch

Windows Azure has seen some updates over the past weekend. One small update is that the Service Bus and Access Control Service are no longer marketed inside the Windows Azure AppFabric brand but are now a substantial part of Windows Azure core.

The Windows Azure Service Bus has two basic feature sets:

  • relayed messaging (through the Service Bus Relay service)
  • brokered messaging (through queues, topics, subscriptions (and the deprecated message buffers)

In this post I show you how to use part of Service Bus’ REST API to create a cross-platform & cross-location publish & subscribe app by leveraging topics and subscriptions.

First of all, let’s launch the wonderful Service Bus Explorer tool and look at my Service Bus namespace:

image

As we can see there are no topics and subscriptions (and also no queues).

What we are going to do is have a .NET console app running on Windows to create a topic and a subscription on the Service bus with the REST API and send messages to the topic. Here is the essential piece of code to do this:

   1:  var broker = new BrokeredMessaging (serviceNamespace);
   2:              
   3:  try
   4:  {
   5:      token = broker.GetToken (issuerName, issuerSecret);
   6:                  
   7:      string topicName = "newstopic";
   8:      string subscriptionName = "iphonesubscription";
   9:   
  10:      broker.CreateTopic(topicName);
  11:      broker.CreateSubscription(topicName, subscriptionName);
  12:                  
  13:      while (true)
  14:      {
  15:          broker.SendMessage (topicName, "Hello " + Guid.NewGuid ().ToString ());
  16:          Thread.Sleep (5000);                    
  17:      }
  18:  }
  19:  catch (WebException we)
  20:  ...


Admitted, the actual heavy work is inside the BrokeredMessaging class. This class is just a simple wrapper around the REST API, and you can see some basic operations using WebClient to talk to the Service Bus in the AppFabric SDK samples (e.g. the Management Operations using REST sample).

The BrokeredMessaging helper class can be found in the sample projects download at the end of this post.

After we retrieved a token from ACS we create a topics and a subscription and send out messages in a loop:

image

These messages are sent to the Service Bus to a durable topic (which uses queues underneath). As long as there is no subscriber which gets and deletes the messages from the subscription we can see the messages sent to the topic in the Service Bus Explorer – here we have sent out 4 messages from the Console application:

image

Cool.

Now let’s use a subscriber to get the messages from the subscription. My goal was to have a native iOS app but built with C# entirely. Therefore I fired up MonoTouch and created a very simple iPhone app. For the super-sophisticated UI I used MonoTouch.Dialog, a highly recommend UI library to quickly create any forms-over-data user interface for iOS apps.

In order to talk to the Service Bus the MonoTouch project was using the exact same BrokeredMessaging class as my Windows console application. The joy of MonoTouch.

Again, the essential code (after getting a token from ACS) to get and delete messages from the SB subscription looks like this (the messages object is the list of elements in the UI to display the messages).

private void RegisterMessagesHandler ()
{
    Task.Factory.StartNew (() =>
    {
        while (true)
        {
            try
            {
                    
                var message = broker.ReceiveAndDeleteMessage (topicName + 
                   "/Subscriptions/" + subscriptionName);
                    
                if (!String.IsNullOrWhiteSpace (message))
                {
                    InvokeOnMainThread (delegate 
                    { 
                        messages.Elements.Add (new StringElement (message));
                        dvc.TableView.ReloadData ();
                    });
                } 
                    
            }
            catch (Exception ex)
            {
                Console.WriteLine (ex);                
            }
        }
    });
}


Note: it may not be wise to store the Service Bus owner and shared secret directly in the device’s app – you know… or at least store it in the Keychain (sample with MT: http://www.wildsau.net/post/2011/02/01/iOS-Store-passwords-in-the-keychain-using-MonoTouch.aspx).

To convince you that there is no magic or cheating going on, this is the code from BrokeredMessaging  to get the message from the subscription (and also delete it):

   1:  public string ReceiveAndDeleteMessage (string relativeAddress)
   2:  {
   3:      string fullAddress = baseAddress + relativeAddress + 
   4:        "/messages/head" + "?timeout=60";
   5:              
   6:      Console.WriteLine ("\nRetrieving message from {0}", fullAddress);
   7:              
   8:      var webClient = new WebClient ();
   9:      webClient.Headers [HttpRequestHeader.Authorization] = token;
  10:   
  11:      var response = webClient.UploadData (fullAddress, 
  12:        "DELETE", new byte[0]);
  13:      var responseStr = Encoding.UTF8.GetString (response);
  14:   
  15:      Console.WriteLine (responseStr);
  16:              
  17:      return responseStr;
  18:  }

Note: Console.WriteLine(…) is the mechanism in MonoTouch to write to debug output. Well…

Whenever we get a real and non-empty message we add it to the list view of our simple iOS app. Voila:

image

And to prove that everything worked as expected, the subscription is now empty and no more messages are in there:

image

Bingo! Smile

Windows Azure Service Bus (together with the Windows Azure Access Control Service) enables us to send messages in an async manner to durable storage in the Cloud and subscribing to those messages (via topics and subscriptions) with literally any device, any platform, from any location!


The sample projects for VS 2010 and MonoTouch can be downloaded:


Hope this helps.

12/06/2011

Claims-based security today - “Can this get even better?” Yes, it can: thinktecture IdentityServer 1.0 released

Dominick Baier, maestro of all-things-security, has finally launched the official successor of our successful thinktecture StarterSTS.
Now, all new, all better Smile

 

thinktecture IdentityServer is an open source security token service based on Microsoft .NET, ASP.NET MVC, WCF and WIF.

High level features

  • Multiple protocols support (WS-Trust, WS-Federation, OAuth2, WRAP, JSNotify, HTTP GET)
  • Multiple token support (SAML 1.1/2.0, SWT)
  • Out of the box integration with ASP.NET membership, roles and profile
  • Support for username/password and client certificates authentication
  • Support for WS-Federation metadata
  • Support for WS-Trust identity delegation
  • Extensibility points to customize configuration and user management handling

Go and grab it, read the docs – and please give feedback.
Thanks!

10/21/2011

Materials for my sessions at Software Architect 2011

As promised in and after the sessions at the wonderful Software Architect 2011 conference in London:

  • Designing & building services the web & HTTP way with WCF 4.5 (Slides  |  Samples [this is the CodePlex Preview 5 branch with samples)
  • Windows Azure platform for Architects (Slides)

Thank you to everybody attending and giving such nice feedback. Hope to see you next time around.

10/03/2011

Unterlagen & Demos zu meinen Sessions & Workshop auf der BASTA! 2011

So – die BASTA! ist vorbei und wir von thinktecture hatten mal wieder eine super Zeit in Mainz. Danke an das Team von S&S Media und vor allem an die Teilnehmer!

Hier finden Sie die Folien und Beispiele zu meinen Sessions und dem Workshop auf der Herbst-BASTA! 2011:

  • Nicht nur Web: Android und iOS Apps mit HTML5 und Co. (Slides | Samples)
  • Kommunikation über die Cloud: Windows Azure AppFabric Service Bus (Slides | Samples)
  • Offen für's "Web": Web APIs mit WCF in .NET 4.0+ (Slides | Samples [this is the Preview 5 branch with samples])
  • Hands-On Windows Azure Platform: Ihr Tag in der Cloud [mit Dominick Baier] (Samples)

 

Wenn Fragen sind wie immer eine EMail an mich (schauen Sie einfach in den Slides nach…). Danke.

Bis bald!

05/10/2011

Erm, what’s happening? Updating Windows Azure role configuration

Sometimes the question shows up why the change of configuration of a Windows Azure deployment does not take effect immediately (whatever that means Smile).

If you change the configuration (via the portal or via the management API) of your Windows Azure deployment then the change will happen almost immediately.
Well, not exactly.

The change will be rolled out to the instances using upgrade domains.

image

Source: Ryan Dunn’s TechEd 2010 presentation “Deploying, Troubleshooting, Managing and Monitoring Applications on Windows Azure”


For each upgrade domain a status change event will be raised and the instance can decide whether it will handle the change or whether it needs to be restarted.
Once the instances in that upgrade domain have reported that they are ready (either by handling the change, or by coming back online after restarting) then the fabric controller will move to the next upgrade domain.

Hope this helps.

04/01/2011

More identity in da house! thinktecture IdentityServer first code drop

Hot, very hot: Dominick just released the all new, all cool (and super modern Smile) thinktecture IdentityServer as a CTP on CodePlex.

Got get it – go and give us feedback.
Thanks!

01/28/2011

thinktecture WSCF.blue has hit the 10,000 downloads landmark on CodePlex

After thinktecture StarterSTS has hit the 5,000 downloads mark a few weeks ago (congrats again, Dom!), we now have hit 10,000 for our WCF-based Web Services contract-first tool WSCF.blue.

[Want to learn more about it and the idea behind it…? Read this MSDN Magazine article]

Thanks to a great team!

image