Thursday, February 23, 2017

Angular CLI–Error when invoking ng

After installing Angular CLI and trying to invoke it using the ng command it failed with the following error message:

Error while running script "C:\Users\root\AppData\Roaming\npm\node_modules\angular-cli\addon\ng2\models\config\config.ts":
SyntaxError: Unexpected token ...
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.require.extensions..ts (C:\Users\root\AppData\Roaming\npm\node_modules\angular-cli\lib\bootstrap-local.js:30:14)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module._load (C:\Users\root\AppData\Roaming\npm\node_modules\angular-cli\lib\bootstrap-local.js:55:22)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Users\root\AppData\Roaming\npm\node_modules\angular-cli\addon\ng2\models\config.ts:7:16)
    at Module._compile (module.js:409:26)
The error message itself didn’t help a lot. Luckily a colleague had the same issue before. He suggested that updating to the latest node.js version would probably solve the issue.
And indeed, an upgrade to Node 6.10(the latest LTS version on the moment of writing) solved the problem…

Wednesday, February 22, 2017

ASP.NET Core–Injecting dependencies in an MVC ActionFilter

ASP.NET Core out-of-the-box supports dependency injection on the action filter level. You have to use constructor injection to inject your dependencies in the actionfilter:

However now it is no longer possible to simply add the action filter as an attribute on top of your controller or action method. This is because attributes must have their constructor parameters supplied where they are applied. Instead you have to use one of the following attributes:

  • TypeFilterAttribute
  • ServiceFilterAttribute

The key difference is that TypeFilterAttribute will find and load the dependencies through DI, and inject them into your action filter.

The ServiceFilterAttribute on the other hand attempts to find the filter from the service collection. This means that you have to register your actionfilter first:


Tuesday, February 21, 2017

Help my ASP.NET Core API returns a 406 status code

Yesterday I got into trouble when invoking a specific REST API created in ASP.NET Core. Instead of getting some JSON data back from the server I got a 406 status code instead.

I had no clue what was causing the issue. Luckily a colleague helped me out. I made a dumb mistake.

I added the [Produces()] attribute to my Web API controller. However I accidently added an invalid content type:

[Produces("listvalues/json")] instead of [Produces("application/json")] Confused smile

Thanks Michael for solving the problem!

Monday, February 20, 2017

Exclude properties in your Swagger OpenAPI metadata

.NET (core) has OpenAPI support through Swashbuckle, a NuGet package that generates OpenAPI metadata based on your API controllers.

I was asked to manipulate the Swagger metadata to exclude a specific property from the document metadata.

I found 2 possible solutions to achieve this goal:

Option 1 – Add a JsonIgnore attribute on your object:

Option 2 – Create a SchemaFilter:

Don’t forget to register the filter in your Swagger configuration:

Friday, February 17, 2017

.NET Core compilation error: Missing compiler required member 'microsoft.csharp.runtimebinder.csharpargumentinfo.create'

After using the dynamic keyword in a .NET core test application, the project no longer compiled. Instead I got the following error message:

Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

The error message itself is not very helpful, but luckily the solution is easy. Just add a reference to Microsoft.CSharp.

Thursday, February 16, 2017

The hardest software problem–Naming things

The hardest problem in software development is naming things. Concepts like the ubiquitous language from DDD could help but still finding the “right” name is hard!

A good video about this topic is “How to Name Things: the solution to the hardest problem in programming” by Peter Hilton.


Wednesday, February 15, 2017

HTTP Security Headers

The HTTP protocol offers a lot of features to improve the security of your web app. If you have no clue what XSS, CSP, HSTS, HPKP,… means, than the following blog post is a must read:

It walks through a whole list of security headers explaining the use case, reasons (not) to use it, and how to activate it on your HTTP server.