I have been working towards my MCSD: Web Applications Certification and am gearing up for the last exam in the series, Developing Microsoft Azure and Web Services (70-487). My experience with WCF has been rather limited so it is something I have been dedicating much of my study time to. At the end of this post I will share several resources I have used for studying but first I wanted to walk through one of the topics that I found very interesting: using the Azure Service Bus to relay a call from the client program to the WCF service.
Getting Setup: Assumptions and requirements
- This tutorial was completed using Visual Studio 2015 Enterprise edition but can be done using the Community edition
- Basic knowledge of creating a WCF service and client is assumed
- See the resources section for links on getting up to speed on WCF
- An Azure account is required
For full disclosure, some of the setup will require services that are not free (more details when we get to that section). You can set up an Azure account for free here and you will receive $200 in credit which will be more than enough for this and many other things. If you already have an Azure account, you can sign up for an MSDN subscription which will give you access to free Azure credits or Microsoft has a Dev Essentials Program which gives you $25 in Azure credits each month for a year among other benefits.
The Azure Service Bus Relay allows a client application talk to a WCF service living behind a firewall. While there are ways to set this up without using a Service Bus Relay, this looks to make it easier to set up and manage.
Using the Service Bus Relay while developing lets you run the service locally while someone outside the firewall – either sitting in a Starbucks down the street, across the country or across the ocean – run the client application. When the client calls the service you will be able to step through the code and see the state of the data sent to the service.
My test was to send the application to a friend in the UK and while we were Skyping I could read back the input he submitted to the application.
For this demo I created a very simple WCF service and client which can be found on GitHub at https://github.com/susanwilliams/MNMBlog-WCFAzureServiceBus. The starting project can be found in the StartingSample branch and the completed sample will be in the master branch.
It is a very simple console app that takes a name input, sends it to the service and receives back the name inputted prefixed by “Hello, “. The initial setup uses a simple console app for self-hosting and uses NetTCPBinding.
Azure Service Bus Setup – Azure Portal
The client and host rely on an access key and an endpoint address based on the Service Bus Namespace you create in Azure.
After getting the sample code or starting with your own the first place we will start is in the Azure portal and set up a Service Bus Namespace.
- In the new portal select ‘Browse’ and then find ‘Service Bus Namespaces’ in the list.
- Select the link on the main window or the “Create” button at the bottom of the page (Selecting the large “New” button in the bottom left will start the quick create process).
- Enter a unique Namespace Name and select Messaging for the Type
This link will take you to the Classic Azure portal. Or you can start from the Classic portal and find Service Bus in the left hand navigation.
I already have one Service Bus Namespace set up but I’ll create a new one for this demo. If you do not have any namespaces created there will be a link in the main window to create a new one.
- The Messaging Tier must be set to Standard. This is where there could be cost associated with the tutorial
- The pricing page for the Service Bus under Relays says that they are only available under the Standard tier and will cost $0.10 for every 100 relay hours and $0.01 for every 10,000 messages
- How much testing you do with this tutorial will determine how much it will cost so it can be minimal but I wanted to be open about the potential costs
- I am using the subscription associated with the Dev Essentials benefit
This was all we needed to do in Azure to set up the relay. Now on to the code.
Azure Service Bus Setup – Code
There are only a couple things to do to update the solution to use the Service Bus Relay.
First, install the WindowsAzure.ServiceBus to both the WCFServiceBus.Client and WCFServiceBus.Host projects.
Next we need to update the endpoints to use the Service Bus. The NuGet package added some relay binding extensions, including netTcpRelayBinding. There are others listed but we are just going to stick with TCP for this post.
If you refer back to the screenshot of creating the Service Bus Namespace, you will see that the name you entered was followed by the note “.servicebus.windows.net”. This prefixed by the name you entered is part of the address that will be used for the endpoint.
In the host and client App.config files the new endpoint configuration looks like this:
---code--- <endpoint address="sb://wcfservicebusmnm.servicebus.windows.net/HelloService" binding="netTcpRelayBinding" contract="WCFServiceBus.Contracts.IHelloService" /> ---/code--- The scheme for the endpoint address is changed to "sb" to specify we are using the Service Bus. It is followed by the uri I referenced to above which uses the namespace you created followed by .servicebus.windows.net. You also need to specify a path for the service – it can be anything but needs to be the same on both client and host. The only other section to add to each configuration file is a new behavior where we will store the access key. ---code--- <behaviors> <endpointBehaviors> <behavior> <transportClientEndpointBehavior> <tokenProvider> <sharedAccessSignature keyName="Your Key Name" key="Enter Your Key Here" /> </tokenProvider> </transportClientEndpointBehavior> </behavior </endpointBehaviors> </behaviors> ---/code---
Once this code is added to the host and client App.config, that’s it! Of course there is more that you can do to build out a full service or even configure it to use credentials but this is the basic setup to use the Azure Service Bus as a relay. You can send the compiled client application to anyone in the world and if they run it while you have the service running on your machine, you will be able to step through the service when it is called.
One way to see the relay working, other than the fact that it is the only endpoint set up in the project is to go back to the Azure portal and in the details for the Service Bus Namespace you created select the Relays tab.
Start the host and put a breakpoint in the implementation of the SayHello operation in WCFServiceBus.Services.HelloManager. Set the Host project as the startup project, begin debugging and run the Client application from the .exe in the Debug folder or set the solution to start with multiple projects.
When your breakpoint gets hit go back to the Relays tab for the Namespace you created in the Azure portal. You will see that it recognizes a relay is being processed.
This is just one of the new things I have learned on my path towards achieving my MCSD certification. It has been great learning new things and how to take services and solutions that have been around for a while and integrate them into Azure.
WCF Study Resources
- Pluralsight Course – WCF End-to-End by Miguel Castro
- Programming WCF Services by Juval Löwy (Book)
Azure Service Bus