Skip to main content

Posts

Showing posts from February, 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/

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

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.

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

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!

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 tok

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.

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 hasC

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 paren

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!

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 grai

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 sho

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.

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 RECOMPIL

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:

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: First  you need to download the Microsoft.AspNetCore.SignalR.Protocols.MessagePack NuGet package. Next s

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…

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