Friday, February 28, 2020

NSubstitute–Checking received calls

Although I prefer stubbing over mocking as much as possible, sometimes it is unavoidable to check the behavior of your test objects. It is a tradeoff to make between rewriting the application logic to make it easier testable or using some mocking magic.

In NSubstitute, the stubbing library of my choice, you can use the Received() or DidNotReceive() extension methods:

More information: https://nsubstitute.github.io/help/received-calls/

Thursday, February 27, 2020

PostgreSQL – Mapping arrays

One of the cool features that PostgreSQL has to offer is the support for array data types. This allows you to store an array in a single column without the need to either use a separate table or concatenate the values in a single  string.

To use this in Entity Framework through Npgsql you simply need to define a regular .NET array or List<> property:

The provider will create text[] columns for the above two properties, and will properly detect changes in them - if you load an array and change one of its elements, calling SaveChanges() will automatically update the row in the database accordingly.

More information: https://www.npgsql.org/efcore/mapping/array.html

Wednesday, February 26, 2020

Using linked files in .NET Core

A long existing feature in Visual Studio is the option to add a file from another location or project as a linked file to your project. If you change the original file the changes are reflected immediately in your project.

It is possible in .NET Core to directly specify this in the csproj. In Include you give the relative path to the file from the project folder, and through the Link property you tell MSBuild to add the file as a link.

Tuesday, February 25, 2020

NPM check - Check for outdated, incorrect, and unused dependencies.

A tip from a colleague; npm-check.

npm-check makes it very easy to see which of your npm packages is outdated, incorrect or unused.

To use it, first install it using:

$ npm install -g npm-check

Then you can call it using:

$ npm-check

Monday, February 24, 2020

npm - Unable to verify first certificate

*WARNING*: Don’t do this at home!

As part of our GraphQL toolchain  we are using graphql-codegen to generate the necessary typescript files from the GraphQL schema and the queries in our application.

GraphQL-codegen is a node.js application that is using graphql-toolkit behind the scenes.

When trying to parse the schema from a site hosted on localhost, the application failed with the following error message;

Unable to verify first certificate

We fixed it by applying a dirty hack:

set NODE_TLS_REJECT_UNAUTHORIZED=0
Never do this on production!

Thursday, February 20, 2020

Angular OIDC - Avoid token sharing

To integrate OIDC in our Angular applications we are using the great angular-oauth2-oidc library from Manfred Steyer.

Once we receive a valid token back from our IdentityServer instance it is stored inside the session storage using the OAuthStorage class. This all seemed to work fine. However we got into trouble when we started to switch between applications. Different Angular apps were receiving a token from another app which resulted into errors.

The problem had nothing to do with the angular-oauth2-oidc library but with the way we had setup our applications. All our applications where sharing the same root url and were hosted as virtual directories inside IIS;

  • apps.example.com/app1
  • apps.example.com/app2
  • apps.example.com/app3

Session storage is scoped per domain but as all applications were using the same domain, they accidently accessed each other tokens.

To fix it we had to create our own OAuthStorage class that used an application prefix to isolate the tokens:

All credits to Jens who investigated the issue and created a fix.

Tuesday, February 18, 2020

Modular Monolith with DDD

Modular monoliths are hot! It combines the modularity of a microservices architecture with the ease of deployment of a monolith.

For a good introduction, have a look at the following video:

If you want to get started in .NET take a look at the following repository on Github: https://github.com/kgrzybek/modular-monolith-with-ddd

It contains a full modular monolith .NET (Core) application created using DDD and CQRS.

Monday, February 17, 2020

GraphQL.NET– Error when upgrading to .NET Core 3.1

After upgrading a .NET Core application to .NET Core 3.1, my GraphQL endpoint started to fail with the following error message:

System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

   at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)

   at Microsoft.AspNetCore.Server.IIS.Core.WrappingStream.Read(Byte[] buffer, Int32 offset, Int32 count)

   at System.IO.StreamReader.ReadBuffer(Span`1 userBuffer, Boolean& readToUserBuffer)

   at System.IO.StreamReader.ReadSpan(Span`1 buffer)

   at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)

   at Newtonsoft.Json.JsonTextReader.ReadData(Boolean append, Int32 charsRequired)

   at Newtonsoft.Json.JsonTextReader.ParseValue()

   at Newtonsoft.Json.JsonReader.ReadAndMoveToContent()

   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)

   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)

   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)

   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)

   at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)

   at GraphQL.Server.Transports.AspNetCore.GraphQLHttpMiddleware`1.Deserialize[T](Stream s)

   at GraphQL.Server.Transports.AspNetCore.GraphQLHttpMiddleware`1.InvokeAsync(HttpContext context)

   at GraphQL.Server.Transports.WebSockets.GraphQLWebSocketsMiddleware`1.InvokeAsync(HttpContext context)

   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)

   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)

   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)

   at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)

   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

To fix it you can apply the suggestion as described in the error message:

But there is a good reason why Microsoft changed the default behavior in .NET Core 3.0 and disabled synchronous IO. It can be a source of thread starvation and application hangs.

The GraphQL.NET team is working on a new 3.0 release specifically created for .NET Core 3.0 but this is still in preview.

You can get the latest pre-release packages from the MyGet feed, where you may want to explicitly pull a certain version using -v.

dotnet add package GraphQL.SystemTextJson -v 3.0.0-preview-1448

Friday, February 14, 2020

Azure DevOps - Assigning work items to a group

In the AssignedTo field inside Azure DevOps you can only select a specific user. This means that you are not able to assign work items to a group(TFS or AD group) out-of-the-box. There is no way to change the existing AssignedTo field but you can add your own extra field:

  • Go to Organization Settings.

  • Click on Process

  • We need to create an inherited process. Click on the next to one of the built-in processes and choose Create inherited process.

  • Specify a name for the inherited process and click on Create process.

  • Now we can add a new field that allows group identities to be assigned. Choose the work item type where you want to add the extra field e.g. Task

  • Click on New field.

  • On the Add a field screen, specify a name and set the field type to Identity.

  • Go to the options tab and check the Allow assigning to groups checkbox

That’s it! The new process can be applied to an existing project that used the same parent process.

Thursday, February 13, 2020

Azure DevOps - Expanded members

Inside Azure DevOps you can see the members of your Team in the Team settings:

On the right side you have a dropdown where you can switch between Direct Members and Expanded Members. But what does this do? I couldn’t find any extra input in the documentation.

This feature is relevant when you add not only users to your team but also TFS groups. In that case when you change to the Expanded Members view, the members in the group are shown as well.

Good to know!

Wednesday, February 12, 2020

Orleans - Observers step by step

One of the features inside Microsoft Orleans is the support for ‘Observers’. Through observers it becomes possible to send asynchronous notifications to one or more clients. It allows you to create a ‘simple’ distributed pub/sub mechanism.

I didn’t find the documentation very clear on this part, so here is a step by step guide on how to implement an Orleans observer:

Step 1  - Create the observer client

An observer is a one-way asynchronous interface that inherits from IGrainObserver, and all its methods must be void. So let’s create this interface first:

Inside our client we need to create a class that implements this interface:

Step 2 – Handling registrations

To handle the registrations we need a grain that can store the list of registered clients. Let’s create a grain interface that can handle the registrations. Let us also add a Publish() method that can be called to publish a message to all registered clients:

Next it’s time to implement the corresponding grain. Inside this grain we use another class to handle the registrations. In previous versions of Orleans you could use the built-in ObserverSubscriptionManager class, but in Orleans 2 and 3 you’ll have to create this class yourself.

Let’s do that first:

Now we can finally create our grain implementation which is rather simple as most of the work is done by the ObserverSubscriptionManager:

The work on the server side is done!

Step 3 -  Link the client to the server

As a last step we need to link the observer client to our SubscriptionManagerGrain. Therefore the client needs to call a static method on the observer factory, CreateObjectReference(), to turn the class into a grain reference, which can then be passed to the subscription method on the notifying grain.

Let’s add an Init() method to do this:

We also add the possibility to unsubscribe and publish a message through the client:

Tuesday, February 11, 2020

Don’t be the hero in your team

You are the problem solver in your team. The moment someone starts a sentence with “I’m wondering how…” or “I don’t understand why…” you are there to help. With a few keystrokes on their keyboard you are solving the hardest problems in your team. For every complex problem you are the ‘go-to’ guy. You are really indispensable. Without you the team is lost and they are all fearing the day you’ll leave…

You are truly the hero in your team. You are feeling great about the value you add to the team and the way you can help everyone. You rock!!!!

Do you recognize this?

Unfortunately I have bad news for you. This is not good at all. By taking on all the hard challenges and solving the problems for your team, you take away their opportunities to grow. You are creating a mindset where nobody even tries to tackle a problem. Instead they immediately face towards you… This is not a healthy situation, your team gets more and more dependent on you. No one will step up as long as you keep showing the same behavior.

It’s time for change. Become a mentor for your team, give help when asked for but don’t offer solutions on a silver plate. Give them all the opportunities to learn and grow.

Make yourself dispensable. Don’t be the hero in your team…

Monday, February 10, 2020

ASP.NET Core - Web.config issue

A colleague asked my help with the following problem; his ASP.NET Core application failed to start. In the event viewer I noticed the following error message:

Could not load configuration. Exception message:
Unable to get required configuration section 'system.webServer/aspNetCore'. Possible reason is web.config authoring error.

Here is the related web.config file:

Did you notice what’s wrong? The web.config includes transformation tags which are not allowed in your web.config file. You should only use them in your transformation files(e.g. web.development.config). After removing the xdt references, the application started without a problem.

Friday, February 7, 2020

SQL Server–Parameter sniffing

A customer contacted me with the following problem:

“We notice vastly different timings when executing the same query over time. Every time we change something small to the structure of the query, the performance is OK again (for a while). ”

This immediately ringed a bell and I expected that there was a problem with the execution plan. And indeed this was confirmed when looking at some of the queries:

Why is this happening?

This is an example of parameter sniffing. The first time a query is ran on SQL server, SQL will generate an execution plan for it and store that plan in the query plan cache. All subsequent executions of that same query will go to the query cache to reuse that same initial query plan — this saves SQL Server time from having to regenerate a new query plan. The problem is that this can lead to a suboptimal plan when there is big variation in the data.

How do I prevent parameter sniffing?

One option you have is to execute the query with the ‘WITH RECOMPILE’ query hint.  This forces SQL Server to generate a new execution plan every time these queries run. The disadvantage here is that we lose all benefit from having SQL Server save CPU cycles by caching execution plans.

Another option is to use the ‘OPTIMIZE FOR’ query hint. You can either choose optimize for ‘UNKNOWN’ or optimize for ‘VALUE’:

  • OPTIMIZE FOR UNKNOWN will use a query plan that’s generated from the average distribution stats for that column/index.
  • OPTIMIZE FOR VALUE creates a plan using whatever parameter value specified. This is great if you know your queries will be retrieving data that’s optimized for the value you specified most of the time.

Thursday, February 6, 2020

Azure DevOps - Add build badge

If you want to show the status of your build in for example a wiki, Azure DevOps allows you to create a build badge.

To create such a build badge, you have to take following actions:

  • Open your Azure DevOps project
  • Go to the Pipelines tab
  • Click on the … in the right corner of the build and select Status Badge

  • Now you can copy the image or markdown to include it anywhere you want:

Wednesday, February 5, 2020

ASP.NET Core - SignalR–MessagePack

Out of the box SignalR uses JSON to serialize/deserialize the data you send over the wire. This is great from a readability perspective and makes it easy to see the sent messages in your favorite browser dev tools. Unfortunately JSON is rather verbose which impacts performance and bandwidth usage.

But did you know that you can also use MessagePack as an alternative?

From the documentation:

MessagePack is a binary serialization format that is fast and compact. It's useful when performance and bandwidth are a concern because it creates smaller messages compared to JSON. Because it's a binary format, messages are unreadable when looking at network traces and logs unless the bytes are passed through a MessagePack parser. SignalR has built-in support for the MessagePack format, and provides APIs for the client and server to use.

Here is how to enable it:

  • On your Javascript frontend you can also use Messagepack through the  @microsoft/signalr-protocol-msgpack npm package:
npm install @microsoft/signalr-protocol-msgpack
  • Now you can update your client configuration and activate the MessagePack protocol:

Be aware that it is not all rainbows and unicorns. Switching to MessagePack is more than changing the protocol. There are some quirks you need to be aware of before making the switch:

https://docs.microsoft.com/en-us/aspnet/core/signalr/messagepackhubprotocol?view=aspnetcore-3.1#messagepack-quirks

Tuesday, February 4, 2020

Dotnet Pack–Set version number

Every .NET Standard Class Library can be packaged as a nuget package through dotnet pack. All information is stored directly inside the csproj file.

It took me some time to figure out how to specify the version for the package.

Probably the easiest way is to embed it directly inside the csproj file by setting the version property:

As I preferred to update the package version through my build pipeline, I didn’t like this approach. Fortunately a good alternative is to use the PackageVersion parameter when executing the dotnet pack command:

dotnet pack -p:PackageVersion=1.1.0

This is a lot easier to embed in your build…

Monday, February 3, 2020

EF Core 3.0–Conventions

After upgrading to Entity Framework Core 3.0 the extension methods I used to manipulate the naming conventions no longer worked.

The RelationalMetadataExtensions class is gone in EF Core 3.0(next to a whole list of other breaking changes).

To fix it I found the EFCore.NamingConventions plugin which also allows you to set your table and column names to snake_case.

Here is my updated DbContext that uses the .UseSnakeCaseNamingConvention():

More information: https://www.npgsql.org/efcore/modeling/table-column-naming.html