Thursday, February 22, 2018

Angular error: Cannot find module '@angular-devkit/core'

Yesterday a colleague asked me for help with the following problem. Angular CLI was working fine on his system(he was using version 1.6.4 on his system), but when he tried to run an Angular project created using an earlier Angular CLI version, he got the following error:

Cannot find module '@angular-devkit/core'

We found 2 solutions that both seemed to work.

Solution 1

Install the @angular-devkit/core package explicitly using 

npm i -D @angular-devkit/core

Solution 2

Upgrade the angular-cli version using

npm update -g @angular/cli

Wednesday, February 21, 2018

Log Parser Studio

The last 2 weeks we had some production issues where an unexpected 401 message was returned from our IIS servers. To further investigate the issue, I got an old tool out of the dust; Log Parser Studio.

Log Parser Studio is a utility that allows you to search through and create reports from your IIS, Event, EXADB and others types of logs. It builds on top of Log Parser 2.2 and has a full user interface for easy creation and management of related SQL queries.

Log Parser Studio allows to query all kinds of log files using a SQL like syntax. It comes out of the box with a large list of sample queries that can help get started.


How to use it

  • Open Log Parser Studio(LPS.exe)
  • Click on the New Query button or choose one of the queries from the library


  • A new query window appears


  • Now it is time to first specify the LogType. Click on the Log Type link in the middle and choose the correct type from the list:


  • Next we have to select the files to query. Click on the Choose Files/folders icon


  • The Log File Manager window appears. Here you can use Add Files or Add Folders to add log files.
    • After selecting some files, click OK to return to the Main screen.


  • Now it is finally time to execute the query. Click on the Execute button


  • After some time(depending on the number of log files and the complexity of the query), the query results are returned:


Tuesday, February 20, 2018

ASP.NET Core - IHttpContextAccessor

ASP.NET Core introduces the IHttpContextAccessor interface as a way to provide access to the HttpContext. Before you can use it, you have to register it at application startup inside the IServicesCollection. During a code review I noticed that it was registered like this:

This turns out to be wrong. The IHttpContextAccessor should be registered as a singleton. So the correct code is the following:

More information:

Monday, February 19, 2018

JSON.NET–Resolve private setters

On one of my projects we are using event sourcing as our data persistence strategy. Based on the event stream I build projections using read models.

I prefer to keep my projected read model immutable but turns out that JSON.NET fails to deserialize the state. All properties retain their default values.

A solution is to create a custom contract resolver that uses reflection to set the property values:

Don't forget to apply this contract resolver using the JsonSerializerSettings:

Thursday, February 15, 2018

PostgreSQL–Generate an auto incrementing number on year basis

For an application we are building we had the following requirement:

A unique increasing number should be generated for every document on a year by year basis. So at the end of each year the document counter should be reset to 0 and start to increase again.

As we are using PostgreSQL as our database, I decided to implement this feature using sequences.

First thing I did was creating a function that generates a new sequence based on a specific name:

This allows me to create a new sequence dynamically at the beginning of each year.

Next thing I did was creating another function that will first invoke the f_create_seq function to see if a sequence already exists and then calls this sequence to get the next number in line.

I invoke this function from my application where I pass a year as the sequence name parameter:

Wednesday, February 14, 2018

Stacktrace demystifier

As the C# compiler gets smarter and smarter and more complex features like iterators, generics, async/await are added, our stack traces become more and more unreadible. This is because the .NET stack traces output the compiler transformed methods; rather than the source code methods, which make them slow to mentally parse and match back to the source code.

A library that can help you get more readible stacktraces is Ben.Demystifier.

Let’s try it:

  • Create a new console application. Set the C# target version to 7.1(Instructions here)
  • Include the Ben.Demystifier nuget package
  • Add the following code:
    • Note that we call the exception.Demystify() extension method to generate a better stacktrace.
  • Let’s now run the application first and compare our stacktraces:

The normal stacktrace(with a lot of compiler noise):

  at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at ConsoleApp1.Program.<Iterator>d__2.MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 35
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at ConsoleApp1.Program.<AsyncCount>d__1.MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 28
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ConsoleApp1.Program.<Main>d__0.MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 15

The demystified stacktrace:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at bool System.Collections.Generic.List<T>+Enumerator.MoveNextRare()
   at IEnumerable<string> ConsoleApp1.Program.Iterator(int startAt)+MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 35
   at int System.Linq.Enumerable.Count<TSource>(IEnumerable<TSource> source)
   at async Task ConsoleApp1.Program.AsyncCount() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 28
    at async Task ConsoleApp1.Program.Main(string[] args) in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 15

Check out the documentation to see the full list of problems with your default stacktraces that this library solves.