Configure Microsoft Azure WCF Relay in WCF Service hosted in IIS.

Tharaka Madhusanka
8 min readMay 15, 2019

Overview

In this article I’m going to discuss how to configure Azure WCF relay which enables cloud application(Web App, Api App), which is hosted in Azure, to consume WCF Service, which is hosted in IIS. And next, in here I discuss how to modify the Web.config in WCF Service, in order to expose the service to the cloud application through Azure WCF Relay. I had to spent bit long time to set up this, according to the scenario I had to work. When I started I had no prior experience or deep knowledge on WCF Service, honestly No Need to know ;-), but it’s better if you have some knowledge about WCF, especially WCF bindings. In this article I am not going to discuss about Azure Relays and WCF Service. And I assume that you have an understanding What is the purpose of using Relays :)

Objectives

In this article you will learn how to,

  • Expose WCF service, hosted in IIS Server, to Azure WCF Relay, using WCF bindings instead of C# code base. There are several articles describe how to set up this through C# code.
  • Communicate to WCF Service through Azure WCF Relay from MVC Web API as the Client.

Prerequisites

The following is minimum required to complete this exercise.

  • Basic understanding of WCF Service and MVC Web API.
  • An Azure Subscription and Basic understanding of Azure, Subscriptions, Resource Groups etc.
  • Visual Studio 12 or Later. I tested this in VS 15 & 17.
  • Installed Nugget Package Manager.

Okay :) Now let’s see how we can set up this.

Overview Architecture

image 0. Azure Relay Communication Overview

I divide this exercise in to main 3 steps.

  1. Create Azure WCF Relay in Azure Portal.
  2. Create WCF Service in Visual Studio
  3. Create WCF Client, in here MVC Web API.

That’s it :)

Task 01 — Create Azure WCF Relay in Azure Portal.

a. Go to your Azure portal. In the Search, type Relay. In the Searched Results List, in Services Category, Select Relays.

image 1. Select Relays

b. When you select Relays, it asks details to create new Azure Relay. Fill in your details, and select Create.

image 2. Create Azure Relay Namespace

So far so good right ! Cool :D

c. Now your relay is created. Click it and then you can see the Relay Dashboard.

image 3. Relay Dashboard

d. Select WCF Relay. click +WCF Relay. Enter your Relay Name, and select the Communication type of your relay. Check Requires Client Authorization. Then Create.

image 4. Create Relay

Note: In this example I am using HTTP Relay. You can use netTcp relay type and you have to use the connection strings accordingly :)

Here We Create the Relay ! So, what’s next?

e. To view the details of the Relay that has been created, Click on the Created Relay. Now you can see the following,

image 5. Created Relay Details

In a temporary location, save the WCF Relay Url. Next go back to your relay dashboard, and select Shared access policies, in the left panel. Here, we’re using Client Authorization, so we need to know few more details.

f. Now go to Relay Dashboard, in the left panel, select Shared Access Policies.

image 6. Shared Access Policies

Note: If you want, you can create different policies for Manage, Listen or Send accesses. But in this example, I used the default, RootManageSharedAccessKey Policy.

g. Click on the Policy. You can see the Connection String and Authentication Keys to access the relay, Copy and Save in temporary location, one of the Primary or Secondary Keys and Connection Strings.

  • Policy Name — RootManageSharedAccessKey.
  • Policy Key — [What you have copied]

YaaY ! Now have completed the Task 01 :D. So, let’s start Task 02.

Task 02 — Create WCF Service, & Configure the Azure WCF Relay

a. Open Visual Studio. Create WCF Project. In my example, I named it as WCFService. So throughout this example, I use this WCFService as my WCF Service project.

b. Now, rename IService1.cs, to IServiceContract.cs. And rename Service1.svc.cs to ServiceWcf.svc.cs

c. Go to your Web.config. There you can see <system.serviceModel> looks like the following.

image 7. system.serviceModel tag

d. Now, go to Nugget Package Manger. Search WindowsAzure.Service.Bus. Install it. After the successful installation, open Web.config again. Check for the <extensions> tag in the body of the <system.serviceModel>. These extensions are added by the above nugget.

image 8. Updated Web.config

Note: Better you use Package Manager Console, and install the WindowsAzure.ServiceBus, as there are some misleading nuggets in the searched results.

Install-Package WindowsAzure.ServiceBus -Version 5.2.0

Note: If you don’t see the above extensions are added, try the following steps. (Actually what I did ;-) )

Go to Nugget Package Manager, go to Installed, Search WindowsAzure.ServiceBus. Now downgrade the installed version. After the downgrade, go to the Updates and update all nuggets. After it’s done, go again to the Web.config, and confirm that the above extensions are added!

What are those extensions? Those are different types of protocols used to communicate to WCF Relay. In this example I use the basicHttpRelayBinding protocol, as we have selected the Relay Type as HTTP.

e. Open Web.config. Under the </servicebehaviour> add the following.

<endpointBehaviors>
<behavior name=“ServiceBehaviour”>
<transportClientEndpointBehavior>
<tokenProvider>
<sharedAccessSignature keyName= [Your Policy Name] key=[Your Policy Key]/>
</tokenProvider>
</transportClientEndpointBehavior>
</behavior>
</endpointBehaviors>

Replace [Your Policy Name] and [Your Policy Key] with what we have saved above, when creating the relay in Azure Portal.

f. While you are still in Web.config, under <appSettings> add the following.

<add key="Microsoft.ServiceBus.ConnectionString"
value=[Your Relay Service Connection String]/>

Replace, [Your Relay Service Connection String], with the connection string of the relay you created, as we discussed above.

g. One more in Web.config, add the following under <system.ServiceModel>.

<services>
<service name="WcfService.ServiceContract">
<endpoint address=[ Url To your WCF Relay]
binding="basicHttpRelayBinding"
contract="WcfService.IServiceContract"
behaviorConfiguration="ServiceBehaviour"/>
</service>
</services>

Replay, [ Url To your WCF Relay] with your relay’s url, what we discussed above.

Note: I assume that you’d have an idea on setting name and contract attributes in WCF Service tag.

h. Now open the IServiceContract.cs and add the following code snippet.

// Interface To Set Set Up The Communication Channel with the WCF Client
interface IServiceChannel : IServiceContract, IClientChannel
{
}

Everything is perfect. Now we have completed the WCF Service Configurations, to consume Azure Relay. So, Let’s press f5 :D

OUCH ! Got Hitched ! Anything we missed ? :’(

It says,

image 9. Exception

What are these ‘AddressAlreadyInUseException’ and ‘isDynamic’?

To Shared Access Signature Keys, it has to be specified that the bindings are not dynamic. So, in here we have to explicitly say that bindings are not dynamic or we have to set this IsDynamic property as false and should bind to our basicHttpRelayBinding. Will see how we can do this.

i. Open again Web.config. under <system.ServiceModel>, add the following code snippet.

<bindings>
<basicHttpRelayBinding>
<binding name=“HttpRelayConfiguration” isDynamic=“false” >
</binding>
</basicHttpRelayBinding>
</bindings>

Note: isDynamic should be isDynamic as it’s case sensitive.

j. Now modify <services>…</services> as following.

<services>
<service name="WcfService.ServiceContract">
<endpoint address=[ Url To your WCF Relay]
binding="basicHttpRelayBinding"
contract="WcfService.IServiceContract"
behaviorConfiguration="ServiceBehaviour"
bindingConfiguration = "HttpRelayConfiguration" />
</service>
</services>

It’s all. Press F5. YAAAY ! Now service is up & working :D :D

image 10. WCF Service is successfully run

Wait ? Yes ! The service is working, but, how we get sure whether it’s exposed to the Azure Relay ?

It’s nothing. Go to your Relay dashboard again in azure portal. There you can see the Number Of Listeners are 1. Not sure, it’s your locally running service ? Okay. Shut down the locally running service, and then go the Azure Portal, Refresh and Check. It’s 0.

image 11. Relay Listeners

Everything is Cool up to now, right ! :D We have one more thing left. Let’s create Client and use the Relay to communicate WCF Service.

Task 03 — Create WCF Client to consume service through Azure Relay.

In this example I am going to create Web API Project, to consume WCF Service. You can create any kind of client to communicate with Azure WCF Relay. As we have set Authorization Required in our Relay, important thing is, to communicate with the relay, have to send the Shared Access Signature Token to establish the communication with the relay.

a. Open Visual Studio. Create New Project. Select ASP.Net Web Api. Then, go to Nugget Package Manager, Search WindowsAzure.ServiceBus install. Follow the same steps in Task 02 - (d).

b. Now We have to add our WCF Service Interface, to our Client project. Add as a Link, the IServiceContract.cs file in WCFService project, to our client project.

Note: The reason of adding it, as WindowsAzure.ServiceBus has IClientChannel interface which set ups the environment and configurations to communicate WCF service, through Azure Relay, from the client side.

c. Now add following code snippet to your API Method.

// Create Shared Access Signature Token
ChannelFactory<WcfService.IServiceChannel> channelFactory;
channelFactory = new ChannelFactory<WcfService.IServiceChannel>(new BasicHttpRelayBinding(),“[Your Relay Url]");
channelFactory.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior
{
TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(
“[Your Policy Key Name]”, “[Your Policy Key]”)});
// Create the Communication to the WCF
WcfService.IServiceChannel channel =channelFactory.CreateChannel();
// Call the Service Method using Channel
return channel.Get();

Replace, [Your Relay Url], [Your Policy Key Name] and [Your Policy Key] with your relay values. Done ! That’s all . Run Both WCF Client Project and WCFService Project. That’s all. Now call your API method, and see the result :D

So, What’s Next ?

image 12. Final Result

Yeah ! I really need a beer Now :D So, You enjoyed ? Then Clap, Share & Thumbs Up. Any Questions or You wanna Add More or You have something to Say, then Comment. You’re Welcome ! :)

I share the Demo Project I discussed in this example, Here !

References

  1. https://docs.microsoft.com/en-us/azure/service-bus-relay/relay-what-is-it
  2. https://docs.microsoft.com/en-us/dotnet/framework/wcf/whats-wcf
  3. https://docs.microsoft.com/en-us/azure/service-bus-relay/relay-wcf-dotnet-get-started
  4. https://www.codit.eu/blog/securing-azure-service-bus-relay-endpoints-with-sharedaccesssignatures/
  5. https://www.sanjaybhagia.com/2018/11/28/accessing-wcf-service-via-azure-service-bus-relay-with-net-core/

--

--