Azure Client based Applications

I’ve been wanting to create a WPF or Console application that could communicate with Azure by passing a Message to the Fabric. However, I did not want to have to pass WCF messages to a worker role as I had seen people doing in previous examples, instead I wanted to use the REST API to send messages straight from the application to the Azure Worker Role.

The easiest way of doing this, is by referencing the currently available StorageClient.dll library that is supplied with the Windows Azure SDK. Then add a App.config file to the WPF project that you want to communicate with the cloud.

Within the App.Config’s appSettings element include an add element for each of the pieces of data that would normally be supplied within the configuration file (cscfg) of the cloud application.

    1: <?xml version="1.0" encoding="utf-8" ?>
    2: <configuration>
    3:   <appSettings>
    4:     <add key="AccountName" value="devstoreaccount1" />
    5:     <add key="AccountSharedKey" value="..." />
    6:     <add key="BlobStorageEndpoint" value="https://127.0.0.1:10000" />
    7:     <add key="QueueStorageEndpoint" value="https://127.0.0.1:10001" />
    8:     <add key="TableStorageEndpoint" value="https://127.0.0.1:10002" />
    9:     <add key="ContainerName" value="domgreenmessage" />
   10:   </appSettings>
   11: </configuration>

Now with these settings added we can use the StorageClient library just as we were doing from the web and worker roles within the online app’s.

    1: namespace WpfAzureApp
    2: {
    3:     using System.Windows;
    4:     using Microsoft.Samples.ServiceHosting.StorageClient;
    5:  
    6:     public partial class Window1 : Window
    7:     {
    8:         public Window1()
    9:         {
   10:             InitializeComponent();
   11:         }
   12:  
   13:         private void Button_Click(object sender, RoutedEventArgs e)
   14:         {
   15:             StorageAccountInfo account = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();
   16:             QueueStorage queueService = QueueStorage.Create(account);
   17:  
   18:             MessageQueue q = queueService.GetQueue(StorageAccountInfo.GetConfigurationSetting("ContainerName", null, true));
   19:             if (!q.DoesQueueExist())
   20:             {
   21:                 q.CreateQueue();
   22:             }
   23:  
   24:             q.PutMessage(new Message(this.txtInput.Text.ToString()));
   25:  
   26:             this.txtInput.Text = string.Empty;
   27:         }
   28:     }
   29: }

(I know this may be fairly trivial for expert devs, but for the less experienced developer this could save a lot of time and trouble.)

Technorati Tags: Azure,Cloud Computing,WPF,Window Presentation Foundation