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

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.

    public interface ICustomerService
        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)

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 ūüôā


Accessing Azure Analysis Services Models using .NET Core

Azure Analysis Services is a fully managed platform as a service (PaaS) that provides enterprise-grade data models in the cloud. Use advanced mashup and modeling features to combine data from multiple data sources, define metrics, and secure your data in a single, trusted tabular semantic data model. The data model provides an easier and faster way for users to browse massive amounts of data for ad-hoc data analysis.

Refer to Microsoft official documentation to read more about Azure Analysis Services

Programming against Analysis services is nothing new and we have been doing it for a long time with the full .NET framework, the most common approach is using ADOMD.Net. In this blog post, I will go through the process of getting the same task done with .NET Core.¬† For this sample, I’m¬†using .NET Core 2.1.

Look at my GitHub repository for the entire source code for this blogpost

The important thing to note here is that there is no official nuget package from Microsoft for ADOMD.NET yet, but I found an unofficial package here (Unofficial.Microsoft.AnalysisServices.AdomdClientNetCore) and it seems to work for my quick test (you have to make a call if you want to use it in production or not). I couldn’t find any official word on this anywhere I looked for.¬†¬†Besides this nuget¬†package for .net core rest of the stuff should work same in full framework (with official nuget¬†for ADOMD.NET)

I have divided this into several steps so that it is easy to follow. So let’s get started!

Step 1: Create Azure Analysis Service resource

The very first thing we need is the Analysis Server and model in Azure. Follow this quick starter to create the server.

Next is to create a model which we will use to query. You can create a model with sample data (adventure works) right from within your Analysis Server.

Click ‘Manage’ in the blade and click ‘New Model’. Select ‘Sample data’ from the drop down and press ‘Add’. It should add the¬†model for you.¬†

Create a model Analysis Services
Model successfully created

Step 2: Create App Service Principal

There are many ways to access analysis services. Simplest is usig a connection string that has usrename and password. But this is not recommended approach and works only with full .Net framework but not .Net Core (I was pointed by bdebaere in his GitHub respo regarding this), so we want to authenticate with other OAuth flows. For this post, we will use token-based authentication. For this we will need an app principal (or Azure AD App)

  1. Sign in to Azure Portal.
  2. Navigate to Azure Active Directory -> App Registrations and Click New application registration.
  3. Register an app with the following settings:
    • Name: any name
    • Application type: Web app/API,
    • Sign-on URL:¬† (Not really important here, you can provide any valid url).
  4. Once the app is created, navigate to ‘Keys’ and add a new key
    • provide the description and select duration and press Save button
    • after that you will be able to see the key it will appear only once¬†so take note of this key and we will use this later on
  5. Also take note of the Application Id from the main page of the application
Setting access key for Azure AD App

Step 3: Assign your user as Service Admin in order to connect from SSMS

Registering an app is not enough. We need to assign access to this app on  Analysis Service Model (adventureworks model that we created in previous step). In order to give this access, we will need SQL Server Management Studio. 

Before we could use that, we need a way to connect to this analysis services instance via SSMS. For this, we need to set up our account as Service Admin. Navigate to the Analysis Services resource that we created in the first step. Click ‘Analysis Services Admin’. Normally your subscription account is set as the admin (this is what I will be using) but you are free to set up any account you wish appropriate.¬†

Setting Sevice Admin for Analysis Service

Step 4: Grant permissions to app principal on the model

  1. Connect to Analysis Service using SSMS 2017 with your account that you assigned as Service Admin in the previous step
    • You will need the Server name (from Step 1)
  2. Select the database model and click on Roles or add a new Role
    • Choose any name¬†
    • Select ‘Read’ database permission for the role¬†
  3. Add the Service principal to any role in below format (search for the app name)
Connect to Azure Analysis Server with Service Admin account
Adding App principal as a member in newly defined role

This will add user with following convention:  app:<appid>@<tenantid>

appid: is the application id for your app you created in the Step 2.

tenantid – is the id of your subscription (you can find this in Properties of your Azure Active Directory)

This didn’t work for me when I tried¬†¬†to use Azure subscription with my personal account (hotmail) so I had to use my company account subscription to make this work.¬†

Step 4: Write the code to access data

Now we are all set up write our code that reads from the Model. Please refer to the entire source code in my GitHub respository

Important method here GetAccessToken.¬†I’m using ADAL.Net (nuget: Microsoft.IdentityModel.Clients.ActiveDirectory) to grab the token for the service principal from Azure AD.¬†

Method to acquire token from Azure AD to access analysis services

Once we have the token, we are good to access data from the model. Here I’m¬†using the unofficial NuGet package for ADOMD.NET that I mentioned previously.¬† The correct¬† Connection String format is:¬†

“Provider=MSOLAP;Data Source=<url of the Azure Analysis Server>;Initial Catalog=<modelname>;User ID=;Password=<access token here>;Persist Security Info=True;Impersonation Level=Impersonate“

User ID is left empty and Password is the access token which we get from Azure AD. 

Method to read data from Analysis Services Model adventureworks

If you run this, you will see the output in the console

Final output of the program

Have you tried to work with Azure Analysis services in .NET Core? How was your experience? I would be very interested in listening to your experience and challenges.