Thursday, September 29, 2016

C# 6.0 Dictionary Initializer

C# 7.0 is on the horizon but I’m still getting used to some of the new features of C# 6.0. One of the features I didn’t use until recently were dictionary initializers.

Before C# 6.0, you had to do something like this:

In C# 6.0, you could separate keys from values by using the indexer syntax making the code easier to read and understand:

Wednesday, September 28, 2016

SQL Azure: How many DTUs do I need?

If you are using SQL Server on premise today and want to move your database to SQL Azure on the cloud, the first questions you need to get answered are:

“Which service tier and performance level should I use?”

“How many database throughput units(DTUs) do I need?”

“And what is this DTU actually?”

These questions are not that easy to answer as the performance characteristics of your SQL Server on premise are different from SQL Azure.

Let’s first answer the question what is a DTU:

A DTU is a unit of measure of the resources that are guaranteed to be available to a standalone Azure SQL database at a specific performance level within a standalone database service tier. A DTU is a blended measure of CPU, memory, and data I/O and transaction log I/O in a ratio determined by an OLTP benchmark workload designed to be typical of real-world OLTP workloads.


OK, so a DTU is one number combining a certain amount of CPU power, memory and I/O. That brings us to the next question, how many DTUs do I need?

To answer this question we can call in help from the Azure SQL Database DTU calculator. This website offers a free tool that helps you capture some performance metrics of you current on-premise environment.  The result can be uploaded to the website and you can see how the database resource consumption matches fits within the limits of each Service Tier/Performance Level.

Great help!

Tuesday, September 27, 2016

Azure Service Fabric: Service Endpoint resolution

Last week I spend a lot of time implementing something I thought would be easy, but took me a while in the end. Inside the Azure Service Fabric I have an actor that needs to talk to a web api hosted in a stateless service. To make this work I need to know the web api endpoint uri to call. My first attempt was to use the default HttpClient but this is not such a good idea as the service instances can move between nodes and the uri can change.

So what is the correct way to do this?

First thing we need to do is to create a communication client and implement the ICommunicationClient interface:

This communication client will be created using a communication client factory. The Reliable Services API provides a CommunicationClientFactoryBase<TCommunicationClient>. This class implements a typical fault-handling retry pattern that makes retrying connections to resolved service endpoints easier.

We have to implement this abstract CommunicationClientFactoryBase class to handle logic that is specific to our communication stack:

Finally, an exception handler is reponsible for determining what action to take when an exception occurs:

Now we can call our Web API endpoint in a safe way:

Monday, September 26, 2016

Visual Studio Team Services: Create and use task groups

A feature that was recently added to VSTS are Task groups. Task groups allows you to combine a sequence of build/release tasks into a single reusable task. You can choose to extract the parameters from the encapsulated tasks as configuration variables, and abstract the rest of the task information.

The new task group is automatically added to the task catalogue, ready to add to other build/release definitions.

To create a task group you need to edit a build/release definition. Select the task you want to group together, right click and choose Create task group.


Ensure that all of the tasks you want to include in a task group have their parameters defined as configuration variables such as $(my-value), where you want to be able to configure these parameters when you use the task group. If you specify a value (instead of a variable) for a parameter, that value becomes a fixed parameter value of the task group.

In the Create task group dialog, enter a name and a description for the new task group, and select a category where you want it to appear in the Task catalogue dialog.


The new task group replaces the task sequence in the definition and will be available in the Task catalogue dialog:


Remark: Task groups are stored at project level, and are not accessible outside the project scope.

Friday, September 23, 2016

Azure Service Fabric: Activate an actor on startup

I’m currently working on a project where we are using Azure Service Fabric and the reliable actors component. We had a situation where we wanted to create and activate a set of actors the moment the actor service is started. In our first attempt we added this logic inside the program.cs:

This is actually a bad idea as we are not taking the actor service lifecycle into account. The correct place to activate our actors is inside the RunAsync method of our actor service:

Thursday, September 22, 2016

Fundamentals of Azure: Second Edition

With the fast pace that Microsoft is releasing new features as part of the Azure platform, every Azure book is outdated soon. Michael Collier and Robin Shahan try to keep up and released a second edition of Microsoft Azure Essentials: Fundamentals of Azure.



Download the PDF 

Download the mobile PDF

Download the epub version

Download the mobi version

Wednesday, September 21, 2016

Initialize a net.tcp based WCF service in IIS

Last week I helped a colleague who couldn’t understand why the initialization logic of his WCF service wasn’t invoked. He created a WCF service that he wanted to host in IIS and had put the initialization logic inside the Application_Start method inside the global.asax. The Application_Start method is called when the application is started as part of the ASP.NET pipeline. To be more precisely, it is called when the HttpApplication type is instantiated. This happens when the first HTTP arrives into the application.

However the WCF service wasn’t using HTTP binding but used the net.tcp binding instead(which works perfectly fine in IIS). This also meant that the Application_Start method is never invoked as no HTTP call will ever arrive.

Fortunately, ASP.NET provides a simple hook that works in a protocol agnostic way. The hook is based on the following method:

public static void AppInitialize();

This method can be put in any class that is defined in a C# file in the application’s \App_Code directory. When the AppDomain is started, this method is invoked.

Note: The file must be added to the project as a code file and NOT as part of an assembly.