Tuesday, September 14, 2021

Visual Studio Code–Draw.io extension

While pair programming with a colleague I noticed he was using  draw.io directly inside Visual Studio Code. This was made possible by the following extension: https://marketplace.visualstudio.com/items?itemName=hediet.vscode-drawio

It offers some great features like:

  • Using draw.io offline
  • Work together on a diagram using the liveshare feature of VS Code
  • Link diagram elements to code fragments

I’m a fan!

Monday, September 13, 2021

The ‘async void’ trap

A colleague contacted me that he had trouble using a library I created. This library is used to abstract away the used mailing system. He told me that when the mail couldn’t be send succesfully, the error message got lost and that the library doesn’t return anything useful.

I started by running my integration test suite against this library(these are the moments you are extremely happy that you’ve spend so much time on writing tests) but all tests returned green.

I’ll asked the colleague to send me the code he was using. Do you notice what’s wrong?

The title of this post already gives you the answer. He was using ‘async void’ instead of ‘async Task’. ‘async void’ should only be used by event handlers.

I recommended using AsyncFixer to avoid this kind of issues in the future.

Friday, September 10, 2021

Azure DevOps - User is not available

I got a question from a colleague who was asking how long it took before a user that was added to Azure AD is available in Azure DevOps. The answer is simple; instant.

So why was this person asking this question then? The reason is that there is a difference in behavior depending if the user is already added to the Azure DevOps organisation or not.

If the user is not part of the organisation yet, when you search for the user by firstname and lastname, nothing will be found.

But if you search for the user by his email address, you will find the user.

It is only after the user is added for the first time to an Azure DevOps organisation that you will be able to find a user by his name. Until then you should use the email address to search for the user in Azure AD.

Thursday, September 9, 2021

MassTransit–Message Initializers

Let’s have a look at a small example that shows how to publish a message in MassTransit:

What is maybe not immediatelly obvious in the code above, is that when you call publishEndpoint.Publish<OrderSubmitted> a lot of magic is going on.

The generic argument would make you think that the  Publish method expeccts a message of type OrderSubmitted. But we are passing an anonymous object??? And to make it even stranger, we only have the OrderSubmitted interface, we never created a class that implements it???

What is happening? How this even works? The answer to all this magic are Message Initializers.

When calling the Publish method above, you are using a specific overload:

Task Publish<T>(object values, CancellationToken cancellationToken = default) where T : class;

Message initializers make it easy to produce interface messages using anonymous objects. The message initializers will try to match the values of an anonymous object with the properties of the specified interface. While doing that it can use tricks like type conversions, conversion of list elements, matching nested objects, specify header values and even use invariant variables(guaranteeing that the same value is used everywhere inside a message).

Check out the documentation to learn more about what is possible through message initializers: https://masstransit-project.com/usage/producers.html#message-initializers

 

Wednesday, September 8, 2021

C# switch expressions

A lot of the examples of switch expressions you can find are where a function directly maps to an expression. For example:

This could make you think that you cannot use a switch expression inside a method body. It certainly is possible. The only important thing is that you assign the result of your switch expression to a variable. Let’s rewrite the example above:

Tuesday, September 7, 2021

Copy files through MSBuild

So far I’ve always used xcopy in a pre or post build event to copy files between projects:

But did you know that you don’t need this and that it can be done by standard msbuild features? (By the way the code above doesn’t even work on Linux)

To achieve the same thing you can use the following msbuild configuration in your csproj file:

2 important things to notice:

  1. You can use file patterns in the 'Include' to specify a set of files
  2. You can use ‘LinkBase’ to specify a target folder

Monday, September 6, 2021

Avoid concurrent test runs by using XUnit Collection Fixtures

Last week I blogged about XUnit Collection Fixtures. Something I wasn’t fully aware of but does make sense if you think about it is that by using the [Collection] attribute all tests that share the same context in the same thread. So you don’t need to worry about concurrent test runs.

By default XUnit tests that are part of the same test class will not run in parallel. For example, in the code below Test1 and Test2 will never run at the same time:

If you want to achieve the same behavior while having tests in multiple classes, you can put tests in the same collection: