Accessing WCF Service via Azure Service Bus Relay with .NET Core

Published on Wednesday, November 28, 2018

Check out my GitHub repository if you want to dig into the source code directly.

WCF (Windows Communication Foundation) has served us for a long time when it comes to talking to many LOB systems (SAP etc.). You might have love or hate (though if I have to guess, it would be on hate side) relationship with WCF but it works and at times we don't have any other choice. If you happen to be in a situation where you are talking to legacy systems via WCF, you most likely have a built an API layer on top of it to serve your clients.

I was recently looking to consume WCF services using .NET Core (with the intention that I can migrate my web application to .NET Core). Very quickly I came up to this GitHub repository which was awesome. However, this .NET Core implementation is not in parity with the full framework yet.  I tested it and it worked just fine with standard bindings (http etc). Shane Boyer has a good blog post on this topic if you want to see how to use this.

As far as my needs (this particular scenario) are concerned, I couldn't get it to work for my setup. This is how the setup looks for me. 

The web application (hosted in Azure Web App) talks to WCF Service (running on-premises, hosted in IIS) via Azure Service Bus Relay (in Azure)

So basically, there is no equivalent to basicHttpRelayBinding in .NET Core implementation yet and this is something my WCF service has to enable communication via Azure Service Bus Relay. 

After discussing with a friend of mine, I got a suggestion to try it out with SOAP instead and it seems to work fine! I know it sounds a bit ironic that on the one hand I'm moving to the latest (and greatest) framework for my web application but at the same time, I'm going one step back and using SOAP protocol to make it work - well such is life. I would very much appreciate if anyone can point me to the better solution. but until then, let's continue forward.

So, in this blog post, I will go through how did I manage to call WCF service from my .NET Core Web Application. Again, here is the entire source code

So this is what I'm going to do: 

  • Create Azure Service Bus Relay (read here how to create this)
  • Create a WCF Service (with Azure Service Bus Relay Binding) and publish this locally to run it on IIS
  • ASP.NET Core Web Application calling to WCF Service using HTTP Post SOAP request.

Follow the link I have provided above to create the service bus relay. Let's start with WCF Service. Here is the sample WCF project I build for this demo.  It's a very basic WCF service which has Azure Service Bus Relay binding besides standard HTTP binding (check out the web.config for binding details). I have deployed this in IIS on my local machine. You can clone the repository and add the details of your relay if you want to try it out yourself. It exposes the following contract, which I'm interested in.

[ServiceContract]
    public interface ICustomerService
    {
        [OperationContract]
        string GetCustomerData(string customerId);
    }

When you browse this service from IIS you should see the service like this. At this point, a Service Bus WCF listener should also be registered in your namespace (you can check it in Azure Portal)

WCF Service deployed in IIS with Azure Service Relay Binding
WCF Relay Registered from WCF Service

Now, let's start with the web application. I have created a vanilla ASP.NET Core Web API project (2.1). You can grab the source code from here (and clone it if you want to try it out yourself). Make sure you update settings in appsettings.json file for Azure Service Bus Relay. If you wish you publish this to Azure Web app - add these settings as Application Settings

After creating the project, I added following NuGet package

Nuget Packages: 

  • Microsoft.Azure.ServiceBus

In ValuesController, I have created a GetCustomerData method that takes in customerId as a string.  In here, I'll make a call to WCF Service and return the response I receive from the service (the WCF service returns a dummy response)

[HttpGet("{customerId}")]
public async Task<string> GetCustomerData(string customerId)

Then moving forwards,  I generate the token for Azur Service Bus Relay:

Generating Token for Service Bus Relay

With this information, I construct the HTTP Post request like this:

We are basically making an HTTP Post Request to the Service Bus Relay endpoint over SOAP. For this, we need to set up a few headers

  • ServiceBusAuthorization - Relay Token retrieved earlier
  • SOAPAction - The Operation you want to call from WCF Service
  • Accept header - text/xml

Lastly, we need to set the body for the request - since this is a SOAP call, we need a SOAP message.

SOAP message body

Press F5 and run the web application, pass in cusotmerId parameter and you should get a response from the service:

Successful response from WCF Service

Have you tried to consume WCF Service from .NET Core application? How did it go for you? I would love to hear from you and would appreciate if you could suggest a better way of doing this as of today? I guess I don't need to tell you that I don't like using SOAP :)

Cheers.



comments powered by Disqus