Friday, December 22, 2017

WSFederation OWIN - Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.0.0.127, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

At one of my clients we are (still) using ASP.NET MVC 5 and Web API 2. To secure these web applications we use the WSFederation OWIN middleware together with ADFS. This combination works nice and helps us keeping our applications secure.

Today one of the teams contacted me and complained that the middleware no longer worked. The error message they got looked like this:

Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' from assembly
'System.IdentityModel.Tokens.Jwt, Version=5.0.0.127, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35'.

The root cause of the problem could be found in the version number, they accidently upgraded the System.IdentityModel.Tokens.Jwt assembly from version 4 to 5. It turns out that version 5 is no longer compatible with OWIN.

After reverting back to version 4, everything returned back to normal…

Thursday, December 21, 2017

TFS 2017 Build- MSTest v2 tests are not recognized

After upgrading our unit tests to MSTest v2 we noticed that our tests were no longer discovered by the VSTest task on our build agent.

As a solution, we decided to invoke the test execution ourself.

Therefore I added 2 tasks to our Build definition:

  • One command line task to execute dotnet test

image

  • One task to collect and publish the test results

image

In the command line task I configured the following settings:

  • To execute the dotnet command we specify ‘dotnet’ as the Tool
  • We also specify the following arguments:
    • test: we want to execute the test commando
    • --no-restore: the package restore already happened in a previous build step and shouldn’t be re-executed here
    • --no-build: assembly compilation already happened in a previous build step and shouldn’t be re-executed here
    • --logger:trx: output the test results in the trx format
  • A last important setting that we change is the ‘Continue on error’ setting is set to true. If we don’t do this, a failing test will stop any further execution of the other build steps so we never get a chance to publish the test results.

clip_image002

In the publish test result task I configured the following settings:

  • Test Result Format: VSTest; Our tests are executed using MSTest and published using the VSTest format.
  • Test Results Files: **/*.trx: Search for all trx files found and publish them
  • Merge Test Results: True; Merge all test results if multiple files are found.

clip_image002[5]

After configuring these steps, we were able to succesfully run our tests and publish the results.

Remark: We are still using TFS 2017 Update 1, a newer version of the Test task exists where this problem is gone.

Wednesday, December 20, 2017

Designing, building, and operating microservices on Azure

Perfect reading material for during the Christmas holidays! Microsoft released some new material together with a reference implementation on how to build a microservices application on top of the Azure platform.

Microservices have become a popular architectural style for building cloud applications that are resilient, highly scalable, and able to evolve quickly. To be more than just a buzzword, however, microservices require a different approach to designing and building applications.

In this set of articles, we explore how to build and run a microservices architecture on Azure. Topics include:

  • Using Domain Driven Design (DDD) to design a microservices architecture.
  • Choosing the right Azure technologies for compute, storage, messaging, and other elements of the design.
  • Understanding microservices design patterns.
  • Designing for resiliency, scalability, and performance.
  • Building a CI/CD pipeline.

Tuesday, December 19, 2017

VS Code–Import Cost extension

The more I use VS Code, the more I love it. It is fast and offers an ever growing list of great extensions. One of the extensions I added recently is the Import Cost Extension.

From the documentation:

This extension will display inline in the editor the size of the imported package. The extension utilizes webpack with babili-webpack-plugin in order to detect the imported size.

 Example Image

On one of the Angular projects I’m working I saw our (minified) vendor.bundle.js file growing to 8MB(!) in size. People were importing any library they found useful without being aware of the extra cost it introduces. With the help of the Import Cost extension, you see the cost early and maybe think twice before you import another library.

I’m a fan!

More information: https://hackernoon.com/keep-your-bundle-size-under-control-with-import-cost-vscode-extension-5d476b3c5a76

Monday, December 18, 2017

Angular–CompareWith

Angular 4 introduces a new directive that helps you compare select options. This directive is especially useful when your select options are dynamically populated.

Let’s explain why…

Here is a sample component that contains a dropdown with some options:

We set the default option by passing the object reference through the FormControl constructor. Problem is now that when we repopulate our options list(for example through an HTTP call), the object reference is gone and the model binding of our selected value is lost.

To solve this problem we can use the compareWith directive which will no longer compare the object references but uses a boolean expression or function instead:

Friday, December 15, 2017

Error CS1525: Invalid expression term 'throw'

On our build server we noticed that one of our builds failed with the following error message:

Error CS1525: Invalid expression term 'throw'

When building the project locally in Visual Studio, we had no errors Sad smile?

We found a solution here that worked for us as well:

Update Microsoft.Net.Compilers to 2.0.1 or greater.

We did an Update-All of the NuGet package at the solution level after which the error disappeared…

Thursday, December 14, 2017

Create a .npmrc file on Windows

Kind of stupid but Windows doesn’t like it when you try to create a file with only an extension(like .gitignore, .npmrc,…). Windows will give you an error message instead:

image

The trick to get it working is to include an ending dot also, like

.npmrc.

Don’t ask me why it works…

Wednesday, December 13, 2017

Lettable operators in RxJs

After upgrading to Angular 5 (and having some trouble with RxJs but that is for another post), I noticed the introduction of "lettable operators", which can be accessed in rxjs/operators.

What is a lettable operator?

A lettable operator is basically any function that returns a function with the signature: <T, R>(source: Observable<T>) => Observable<R>. Euhm, what?!

Simply put, operators (like filter, map, …) are no longer tied to an Observable directly but can be used with the current let operator(explaining the name). This means you can no longer use the dot-chaining, but will have to use another way to compose your operators.

Therefore is a pipe method built into Observable now at Observable.prototype.pipe:

Why lettable operators?

Lettable operators were introduced to solve the following problems with the dot-chaining(from the documentation):

  1. Any library that imports a patch operator will augment the Observable.prototype for all consumers of that library, creating blind dependencies. If the library removes their usage, they unknowingly break everyone else. With lettables, you have to import the operators you need into each file you use them in.
  2. Operators patched directly onto the prototype are not "tree-shakeable" by tools like rollup or webpack. Lettable operators will be as they are just functions pulled in from modules directly.
  3. Unused operators that are being imported in apps cannot be detected reliably by any sort of build tooling or lint rule. That means that you might import scan, but stop using it, and it's still being added to your output bundle. With lettable operators, if you're not using it, a lint rule can pick it up for you.
  4. Functional composition is awesome. Building your own custom operators becomes much, much easier, and now they work and look just like all other operators from rxjs. You don't need to extend Observable or override lift anymore.

In short lettable operators will improve tree shaking and make it easier to create custom operators.

Tuesday, December 12, 2017

PostgreSQL: Identify your slowest queries

PostgreSQL provides a large list of modules that extends its core functionality. One of these modules is the pg_stat_statements module that provides a means for tracking execution statistics of all SQL statements executed by a server.

Before you can use this module, it must be loaded by adding pg_stat_statements to shared_preload_libraries in postgresql.conf, because it requires additional shared memory. This means that a server restart is needed to add the module.

After loading the module you can execute the below query to get the top 5 duration queries executed during your performance/benchmarking run:

It is recommended to reset the pg_stat_statements using the query below to ensure that you only capture the statements from your performance/benchmarking run:

Monday, December 11, 2017

System.Data.SqlClient.SqlException : The ntext data type cannot be selected as DISTINCT because it is not comparable

After upgrading to NHibernate 5, one of our integration tests started to fail with the following error message:

System.Data.SqlClient.SqlException : The ntext data type cannot be selected as DISTINCT because it is not comparable.

The error itself happened inside a by Nhibernate generated query:

Message: NHibernate.Exceptions.GenericADOException : [SQL: select distinct supplier1_.SupplierID as supplierid1_138_, supplier1_.Address as address2_138_, supplier1_.City as city3_138_, supplier1_.CompanyName as companyname4_138_, supplier1_.ContactName as contactname5_138_, supplier1_.ContactTitle as contacttitle6_138_, supplier1_.Country as country7_138_, supplier1_.Fax as fax8_138_, supplier1_.HomePage as homepage9_138_, supplier1_.Phone as phone10_138_, supplier1_.PostalCode as postalcode11_138_, supplier1_.Region as region12_138_ from Products product0_ inner join Categories category2_ on product0_.CategoryID=category2_.CategoryID, Suppliers supplier1_ where supplier1_.SupplierID=product0_.SupplierID and category2_.CategoryName=@p0] 

The error message pointed us to a ntext data type. A look into the database revealed that indeed we were using the ntext data type.

clip_image002

It is recommended to avoid this data type(it is marked as obsolete) and use nvarchar(MAX) instead.

But why didn’t we got this error before? It turned out that the query generated by NHibernate 5 is different then the one used by NHibernate 4. In NHibernate 5 an extra DISTINCT clause is added to the query, resulting in the error message above.

Friday, December 8, 2017

Angular 5 - EmptyError: no elements in sequence

After upgrading to Angular 5, the first run of my application ain’t a big success. I ended up with the following error message when I tried to navigate using the Angular Router:

EmptyError: no elements in sequence

The problem turned out not to be related to Angular directly but to RxJS 5.5.3. Reverting to 5.5.2 resolved the problem.

npm install rxjs@5.5.2

More information: https://github.com/angular/angular/issues/20752

Thursday, December 7, 2017

Angular Update Guide

With the fast cadence that new versions of Angular are released, it is not always easy to know what steps are necessary to go from one version to another.

To help you, you can use the Angular Update Guide: https://angular-update-guide.firebaseapp.com/.

This is a small application that asks you the following questions:

  • From what version to what version do you want to migrate?
  • How complex is your app?
  • Do you use ngUpgrade?
  • What Package Manager do you use?

image

After answering all these questions, you can click the Show me how to update! button and you get a step by step guide:

image

Wednesday, December 6, 2017

NHibernate 5–Async/await

With all the fuzz about .NET Core I almost forgot that NHibernate 5 was released a month ago.

One the things I was waiting for was the introduction of async/await to optimize IO bound methods. And after waiting for a loooooong time it’s finally there:

Tuesday, December 5, 2017

TypeScript: Variable 'require' must be of type 'any', but here has type 'NodeRequire'.

After loading a solution for a code review, I was welcomed by the following error message:

Variable 'require' must be of type 'any', but here has type 'NodeRequire'.

Google brought me to the following GitHub issue; https://github.com/Microsoft/TypeScript/issues/16298 where the proposed solution was as simple as beautiful:

There must be another definition of require somewhere.

And indeed, after triggering a search on my solution I found another ‘declare var require’ somewhere in a typedefinition file. After removing it, the error disappeared (and everything still worked).

Monday, December 4, 2017

The request was aborted: could not create SSL/TLS secure channel–Part 3

A colleague asked for help with the following problem:

He has an ASP.NET MVC website that talks to an ASP.NET Web API backend. On development everything works as expected but on the acceptance environment, he suddenly start to get TLS errors when the httpclient invokes a call to the backend:

The request was aborted: Could not create SSL/TLS secure channel.

Let’s take you through the journey that brought us to our final solution.

Part 3 – The registry hack

Our acceptance and production environments are still Windows Server 2008 R2. On these OS TLS 1.0 is still the default. We can change this by altering the registry:

  • Open the registry on your server by running ‘regedit‘
  • Browse to the following location:
    • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols
  • Add the TLS 1.1 and TLS 1.2 keys under Protocols
    • Right click on Protocols and click New –> Key
  • Create 2 keys Client and Server under both TLS keys
  • Create DWORD values under the Server and Client keys
    • Right click on Server and Client and click New—> DWORD
    • Set the following values:
      • DisabledByDefault [Value = 0]
      • Enabled [Value = 1]
  • That’s it!

Friday, December 1, 2017

The request was aborted: could not create SSL/TLS secure channel–Part 2

A colleague asked for help with the following problem:

He has an ASP.NET MVC website that talks to an ASP.NET Web API backend. On development everything works as expected but on the acceptance environment, he suddenly start to get TLS errors when the httpclient invokes a call to the backend:

The request was aborted: Could not create SSL/TLS secure channel.

Let’s take you through the journey that brought us to our final solution.

Part 2 – The unexpected .NET Framework update(again).

As mentioned in Part 1, the problem started to appear after a rollout of .NET Framework 4.6. Now what made it even stranger is that we didn’t saw the issue on our production environment. So why didn’t it work on our acceptance environment and did it work on our production environment?

Turned out that on our production enviroment another .NET Framework update was executed and that the behavior of the HttpClient changed (again):

From the documentation:

Default operating system support for TLS protocols*

The TLS stack, which is used by System.Net.Security.SslStream and up-stack components such as HTTP, FTP, and SMTP, allows developers to use the default TLS protocols supported by the operating system. Developers need no longer hard-code a TLS version.

This explained why it worked on production where the HttpClient no longer used TLS1.2 by default but falls back to the OS default.

But wait, we are still using an older procotol, can’t we change this? This is a question we’ll answer in our third and final post…