Friday, September 30, 2016

API Style Book

Here is a great resource I wanted to share with you: http://apistylebook.com/

image

From the website:

The API Stylebook aims to help API Designers to solve API design matters and build their API design guidelines by providing quick and easy access to selected and categorized resources.

In this first version the API Stylebook provides direct links to specific topics within publicly available API Design Guidelines. Instead of reinventing the wheel or searching google for hours, API Designers quickly can find solutions and inspiration from these existing guidelines.

It brings the shared knowledge of a lot of companies together in one location giving you insight in how other (succesfull) companies build their APIs.

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.

Source: https://azure.microsoft.com/en-us/documentation/articles/sql-database-what-is-a-dtu/

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.

image

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.

image

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

image

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.

image

 

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.

Tuesday, September 20, 2016

Papers we love

One of my goals for this year is to refresh some of my computer science knowledge. So recently I started to read some scientific papers. While browsing the web searching for some interesting content, I stumbled over http://paperswelove.org/

Papers We Love is a repository of academic computer science papers and a community who loves reading them.

image

Great resource offering hours of reading material!

Monday, September 19, 2016

Nice quote–Simplicity is hard

“I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

C.A.R. Hoare

Friday, September 16, 2016

Visual Studio Team Services Hosted Build Agent–Improve build time of your .NET Core applications

When you are using the Hosted build agents(managed for you by Visual Studio Team Services), the agent tries to cache the packages that you are using inside your .NET core app. This makes sense when you re-use the same build agent for multiple builds. But in the situation where you are using the Hosted build agent, you get an agent assigned from a pool meaning that each time you build a different agent can be used. In that case it makes no sense to spend some extra time caching the packages on the machine.

You can disable the caching by setting DOTNET_SKIP_FIRST_TIME_EXPERIENCE environment variable to true on your build machine. This can be done through the variables inside your build definition:

image

A great tip by Donovan Brown to decrease the time to build your .NET Core applications…

Thursday, September 15, 2016

Awesome Domain Driven Design

Just a quick post with some great content I wanted to share:

Nick Chamberlain started a new Github Repository; awesome-ddd; a curated list of Domain-Driven Design (DDD), Command Query Responsibility Segregation (CQRS), Event Sourcing, and Event Storming resources.

This will certainly become my ‘go-to’ starting point whenever I need to find some information regarding DDD. Thanks Nick!

Wednesday, September 14, 2016

Updating Xamarin in Visual Studio

After the announcements of the new Xamarin updates, I thought time to update! And indeed when opening Visual Studio a notification appeared telling me an update was available. But after clicking on the notification I couldn’t find how to initiate the update Confused smile

Turns out that this is actually not that hard:

  • Inside Visual Studio, click on Tools –> Options
  • Search for Xamarin in the Options menu
  • Select Other and click on Check Now to find and install available updates.

image

Tuesday, September 13, 2016

Using Bootstrap Glyphicons in your css

Bootstrap includes a large set of icons based on the Glyphicon Halflings set. Using these icons is easy as you only need to specify some class information in your html:

<button type="button" class="btn btn-default" aria-label="Left Align">
  <span class="glyphicon glyphicon-align-left" aria-hidden="true"></span>
</button>
However I had a situation recently where I didn’t want to specify the icon inside my html but inside my css instead. This is certainly possible by specifying a font-family and set the icon using the content property alongside a unicode hex entity:

span.icon:before { 
  font-family: 'Glyphicons Halflings'; 
  content: "\f015"; 
}

To add this icon to a page, add this HTML tag anywhere in the body of your page:

<span class="icon"></span>
But where does this hex entity comes from? Check out the glyphicons cheat sheet. It allows you to easily copy the related hex code from each icon in the set:
image

Monday, September 12, 2016

Can I port my application to .NET Core?

Interested in porting your existing application to .NET Core(or better said to .NET Standard) but you are wondering how hard the process would be? Check out icanhasdot.net.

It allows you to upload your NuGet packages.config, project.json or paket.dependencies files for analysis and gives you an indication wether a .NET Standard version is available or not:

image

Friday, September 2, 2016

Entity Framework: Timeout when initializing a database on SQL Azure

A colleague came to me with the following problem:

“I’m using Entity Framework and a DropCreateDatabaseIfModelChanges DB Initializer to setup the database. This works perfect locally on my system but when I try to use a database in SQL Azure, I always get a SqlException with the following message:  Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding." How can I fix this?

My first reaction was, don’t do this. I’m not a big fan of the DB Initializers and prefer to generate the DDL and do the rollout using my release pipeline. But during development this could save us some time. So let’s see how we can fix this…

The reason we have this issue is because the creation of a database on SQL Azure can take some time(at least more time than the default timeout(60 seconds)), explaining the Timeout expired message. So the question becomes how can we change the default timeout?

There are multiple timeout values that can play a role:

  • We have the connection timeout. This can be configured in the connection string:
<connectionStrings>
     <add name="MyContext" connectionString="Server=tcp:azure_server_name.database.windows.net,1433;Database=azure_database_name;User ID=azure_user_account@azure_server_name;Password=azure_password;Trusted_Connection=False;Encrypt=True;Connection Timeout=180"
          providerName="System.Data.SqlClient" />
</connectionStrings>
  • We have the command timeout. This can be configured inside our DBContext instance:
public MyContext() : base("name=MyContext")
{
    // three minute command timeout
    this.Database.CommandTimeout = 180;
}

DbMigrationsConfiguration.CommandTimeout=180;//Seconds

Thursday, September 1, 2016

Don’t dispose your HttpClient

.NET allows you to call out to an HTTP service using the HttpClient object. As the HttpClient implements an IDisposable interface and me being a good boy, I always used the HttpClient inside a using statement:

Turns out that this is, in contrast to most situations where a class implements IDisposable, not a good idea to do with the HttpClient. According to this post by Simon Timms on ASP.NET Monsters, each instance of the HttpClient will create a new socket and hold a connection open for a specific interval. This means if you create new HttpClients fast enought sooner or later, you’ll exhaust the connection pool and end up with the following error message:

Unable to connect to the remote server

System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

Instead you should create a single instance of the HttpClient and re-use it everywhere. If you want to learn more, check out Simons post: http://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

And now I have to go. Time to fix this in my code…