Jaap Vossers' SharePoint Blog

Just another WordPress.com site

Archive for the ‘WCF’ Category

Playing with Azure Service Bus Relays, Queues, Cloud Services and WCF

leave a comment »

I have been getting my hands dirty recently working on a little solution which is functionally pretty useless, but demonstrates basic usage of some very cool Azure features, including Azure Service Bus Relays and Azure Service Bus Queues.

Essentially it’s a combination of several applications (some of which are cloud hosted in Azure while others are on-prem components) working together, calling each other in a never-ending cycle, as depicted below.

image

Main components of the solution:

  • Console Application
    • The Console Application is running on-prem (my laptop at home) and is self-hosting a WCF service that that can be invoked via Azure Service Bus. It has a single Web Service method which, when invoked, will use the QueueClient class to add a new message to the Azure Service Bus Queue, which is hosted in Azure.
  • Azure Service Bus Queue
    • The Azure Service Bus Queue is a component that lives inside Azure. Items are being added to this queue from the abovementioned Console Application. The Queue has to be created explicitly, either through the Azure portal, or through code. In our case we are doing it programmatically.
  • Worker Role (Cloud Service)
    • The Worker Role will respond to the OnMessage() event as soon as a new message is detected in the Queue. It responds by invoking the Web Service method that is hosted by the on-prem Console Application.
  • Azure Service Bus Relay
    • The Azure Service Bus Relay is a component that lives in Azure. It doesn’t need to be created explicitly, as it’s created and destroyed automatically by Azure in the background when it’s (no longer) needed.

The most beautiful part of this solution, I find, is the fact that we have an Azure hosted component that can invoke a Web Service that is hosted on-prem, sitting behind a firewall, etc. There is not a huge amount of code involved in getting this setup, as most of the complexity is handled by the WCF binding that is used by WCF service and client.

Think about how powerful this can be in scenarios where a cloud hosted solution needs to access data or otherwise interact with applications that are hosted on-prem, behind a firewall. The key thing here is that the on-prem Web Service actually initiates the connection to the Azure Service Bus Relay and keeps it open. Any incoming Web Service calls will reuse this existing connection.

To get the solution working, follow these steps:

  1. Get source code from GitHub
  2. Create an Azure Service Bus Namespace in your Azure tenant
  3. Update source code with values from your newly created Azure Service Bus Namespace
    • AzureCycle.Cloud.WorkerRole
      1. EndpointAddress address = new EndpointAddress(ServiceBusEnvironment.CreateServiceUri("sb", "TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE", "cycle"));
      2. TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE SharedAccessKey")
      3. <add key="Microsoft.ServiceBus.ConnectionString"
              value="TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE CONNECTIONSTRING"/>
    • AzureCycle.Cloud.WorkerRoleRegistration
      • <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="TODO REPLACE WITH YOUR AZURE STORAGE ACCOUNT CONNECTIONSTRING" />
      • <Setting name="Microsoft.ServiceBus.ConnectionString" value="TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE CONNECTIONSTRING" />
    • AzureCycle.OnPrem.ConsoleApplication
      1. Uri uri = ServiceBusEnvironment.CreateServiceUri("sb", "TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE", "cycle");
      2. TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE SharedAccessKey")
      3. <Setting name="Microsoft.ServiceBus.ConnectionString" value="TODO REPLACE WITH YOUR AZURE SERVICE BUS NAMESPACE CONNECTIONSTRING" />
  4. Publish the Cloud Project (AzureCycle.Cloud.WorkerRoleRegistration) to Azure and ensure it’s started
  5. Start the Console Application (AzureCycle.OnPrem.ConsoleApplication). This will automatically add one initial message to the Queue to start the cycle.

If everything works ok, you should see Console output similar to what can be seen in the screenshot below, where each printed line represents a completed cycle.

image

Written by jvossers

October 28, 2015 at 8:33 am