Monday, January 18, 2021

ASP.NET Core - Set ContentType

Today I was looking at a way to set the correct MIME type for a response based on the file extension.

What is a MIME type?

A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) is a standard that indicates the nature and format of a document, file, or assortment of bytes. It is defined and standardized in IETF's RFC 6838.

How do you set the MIME type in ASP.NET Core?

In ASP.NET core you can set the MIME type of the file you return through the contenttype property on the File method of the ControllerBase class:

How to get the MIME type from a file extension?

My first idea was to write a switch statement with the list of file extension and the corresponding MIME types.  But this turned out not be necessary as the ASP.NET Core team already provides you this functionality out-of-the-box through the FileExtensionContentTypeProvider:

Friday, January 15, 2021

WSFederation is back!

Great news for everyone who has still some WCF services running. Until recently only BasicHttpBinding and NetTcpBinding were supported. This limited the usefulness as most WCF services I encountered where using WSFederationHttpBinding (or the more recent WS2007FederationHttpBinding). These bindings were typically used in scenarios where users authenticate with a security token service (like Active Directory Federation Services).

Last month, the System.ServiceModel.Federation package was released, adding client(!) support for WSFederation.

Remark: You cannot use this binding yet through dotnet-svcutil or the Visual Studio’s WCF Web Service Reference Provider Tool but this will be added in the future.

Thursday, January 14, 2021

NUnit - Combining multiple asserts in one test

The default rule when writing tests is ‘one test, one assert’. There are multiple reasons why this is a good idea:

  1. It becomes very clear what your test is doing and what the success or failure condition is
  2. If there are multiple asserts in your test, the test will fail on the first assert that returns false. The other assertions will never be validated.

However this sometimes lead to duplication where you are writing the same test multiple times to assert different aspects.

In NUnit you can avoid the second reason by using ‘Assert.Multiple()’.

NUnit will store any failures encountered in the Multiple block and report all of them together. If multiple asserts failed, then all will be reported.

Wednesday, January 13, 2021

Integrating the RabbitMQ client in a .NET Core application

I couldn’t find a good example on the best way to integrate the RabbitMQ C# client in a .NET core application. So time to write a post about it.

I would recommend to use the Hosted Services, either directly through the IHostedService or through the BackgroundService.

Let’s see how to do it using the BackgroundService:

  • We create a new class that inherits from BackgroundService:
  • We’ll use the StartAsync method to create the Connectionfactory, Connection and a Channel to listen on:

Remark: Notice the DispatchConsumersAsync = true in the ConnectionFactory configuration. This is important to be able to use an async consumer. If you don’t add this configuration no messages will be picked up by the AsyncEventingBasicConsumer.

  • We also implement the StopAsync method to cleanup when our backgroundservice is shutdown:
  • Now the only thing left is to create a consumer to start receiving messages. We’ll use the ExecuteAsync method for that:
  • Of course we should not forget to register our Hosted Service:

Tuesday, January 12, 2021

Find the multiplier(s) in your team

Remark: This blog post is dedicated to all the team multipliers I had the pleasure to work with over the years.

A na├»ve way to measure the productivity of a developer is to have a look at the individual work output(let’s just ignore how we measure work output in this post, everyone agrees it is number of lines written). The reality is that most software is developed by a team where the work output of each member is a function of work output from all their teammates. Measuring individual productivity can be hard to impossible.

Taking this approach would certainly leave out the most important people in your team; the team multipliers. These people may not accomplish a lot on their own, but their effect on the team’s productivity is exponential. It are those people who can make the difference between success and failure. They’ll typically don’t produce a lot of features on their own, but make the jobs of their teammates a lot easier, are always there to help, willing to do the dirty jobs.

Find the multipliers in your team. Recognize their value and say “thank you”. They are the real heroes in your team…

Monday, January 11, 2021

GraphQL Hot Chocolate–Application Insights monitoring

If you use the default Application Insights configuration to monitor your GraphQL endpoint, there is not that much information you’ll get. For example, when a GraphQL request fails, it still results in a 200 response. Application Insights isn’t aware that you are using GraphQL and don’t flag this request as an exception.

Let’s see how we can improve our Application Insights logging by integrating it with Hot Chocolate.

Disclaimer: the code below has been written for Hot Chocolate 10. In Hot Chocolate 11, the diagnostics system is rewritten. I’ll write an update post when I have the chance to update one of my applications to Hot Chocolate 11.

  • First thing we need to do is to create a class that implement the IDiagnosticObserver marker interface and inject the Application Insights TelemetryClient:
  • For every event that we want to track, we need to add a method and specify the corresponding Hot Chocolate event through the DiagnosticName attribute:
  • We create a dictionary to hold all ongoing requests. This is necessary to update the correct TelemetryClient instance:
  • Now we can see if our GraphQL request has errors and in that case mark the request as an exception:
  • As a last step we need to register our Observer on the ServiceCollection through the AddDiagnosticObserver extension method:

Here is the complete code:

Thursday, January 7, 2021

Visual Studio Code Coding Packs

Microsoft introduced the concept of Coding Packs for Visual Studio Code.

What is a Coding Pack?

Coding Packs are specialized installs of Visual Studio Code that are pre-configured for specific coding environments. They are designed to help students and educators ramp up on VS Code more quickly by automating editor and environment configuration. Coding Packs are a single download that performs the following when installed:

  • Installs VS Code
  • Installs a specific runtime (Python, Java, etc.)
  • Installs essential extensions
  • Ensures installs happen under the user scope so as not to require administrator permissions.

There are especially useful to help you having the necessary tools installed on your student PC’s when giving a training.

Right now a coding pack exists for Python and JAVA but I’ll hope others will be added in the future: