Wednesday, October 31, 2018

Git–Remove local commits

Quick reminder for myself; I wanted to revert my local commits and reset my branch to the status on the origin. I didn’t push my changes yet, so no need to use revert.

As I always forget the correct statement, here it is;

git reset --hard origin/<branch_name>

Tuesday, October 30, 2018

ElasticSearch–Reindex API

At first when I had to recreate or change an index in ElasticSearch I used a rather naïve approach. I deleted and recreated my index and start processing all data again from the original source.

This approach worked but put a lot of stress on the network and ElasticSearch nodes.

A better solution is to use the reindex API. It enables you to reindex your documents without requiring any plugin nor external tool.

Thanks to the existence of the _source field you already have the whole document available to you in Elasticsearch itself. This means you can start from your existing index and use the reindex API to create a new index next to it:

POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

Remark: Reindex does not attempt to set up the destination index. You should set up the destination index prior to running a _reindex action, including setting up mappings, shard counts, replicas, etc.

More information: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html

Monday, October 29, 2018

Advanced async/await using AsyncUtilities

If you want to do some advanced stuff with async/await in C# I can recommend the AsyncUtilities library from Bar Arnon. It contains a set of useful utilities and extensions for async programming.

From the documentation:

Utilities:

  1. ValueTask
  2. AsyncLock
  3. Striped Lock
  4. TaskEnumerableAwaiter
  5. CancelableTaskCompletionSource

Extension Methods:

  1. ContinueWithSynchronously
  2. TryCompleteFromCompletedTask
  3. ToCancellationTokenSource

I especially like the support for TaskEnumerableAwaiter and the nice way he implemented it using the fact that when implementing async/await the compiler doesn’t expect Task or Task<T> specifically, it looks for a GetAwaiter method that returns an awaiter that in turn implements INotifyCompletion and has:

  • IsCompleted: Enables optimizations when the operation completes synchronously.
  • OnCompleted: Accepts the callback to invoke when the asynchronous operation completes.
  • GetResult: Returns the result of the operation (if there is one) and rethrows the exception if one occurred.

Thanks to the fact that GetAwaiter can also be an extension method it becomes possible to turn existing types to awaitables with the right custom awaiter.

Bar did exactly that for awaiting a collection of tasks:

More information: http://blog.i3arnon.com/2018/01/02/task-enumerable-awaiter/

Friday, October 26, 2018

Stay up-to-date on everything what happens in Azure

Microsoft Azure is evolving at an enormous speed making it hard to keep up. The best way to stay up-to-date is to subscribe the Azure newsletter, giving you a weekly updated on everything what’s going on in Azure land.

image

Thursday, October 25, 2018

Owin–Stage Markers

While reviewing some code I noticed the following code snippet;

I had no clue what Stage Markers where so time to dig in…

What are stage markers?

Stage markers play a rol when you are using OWIN in IIS. IIS has a specific execution pipeline containing a predefined set of pipeline events. If you want to run a specific set of OWIN middleware  during a particular stage in the IIS pipeline, you  can use the UseStageMarker method as seen in the sample above.

Stage marker rules

There are rules on which stage of the pipeline you can execute middleware and the order components must run. Following stage events are supported:

By default, OWIN middleware runs at the last event (PreHandlerExecute). To run a set of middleware components during an earlier stage, insert a stage marker right after the last component in the set during registration.

More information: https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline

Wednesday, October 24, 2018

ElasticSearch - Log request and response data

For debugging purposes it can be useful to see the executed REST call. To make this possible using NEST you first have to disable Direct Streaming after which you can start capturing request and response data.

Here is a short snippet on how to enable this in your application:

Important: Don’t forget to disable this when you go to production, it has a big impact on performance.

Tuesday, October 23, 2018

ElasticSearch - Enable HttpCompression

Traffic between your application and ElasticSearch is uncompressed by default. If you want to gain some performance(at the cost of some CPU cycles), it is probably a good idea to enable http compression.

Here is how to enable this using NEST:

Monday, October 22, 2018

Application Insights–Change the application name in the Application Map

When you use the default configuration in Application Insights, the default role name (on-premise) used is the name of your Application Insights resource:

image

This is not that meaningful especially because right now our frontend and backend telemetry is shown together. Let’s fix this by introducing a TelemetryInitializer. In this TelemetryInitializer we update the RoleName:

  • Create an initializer in both the frontend and backend project:
  • Don’t forget to change the role name accordingly.
  • Load this initializer in your global.asax:

· Run the application again

image

Remark: You’ll have to wait some time before the updated names show up on the Application map

Friday, October 19, 2018

Team Foundation Server 2018 Update 3–Securing search

With the release of Team Foundation Server Update 3, security was enhanced by enabling basic authentication between TFS and the Search service. Before there was no security enabled out-of-the-box.

This means that when you try to upgrade to Update 3, you are required to provide a new user/password combination:

tfssearch

More information: https://blogs.msdn.microsoft.com/devops/2018/09/13/search-msrc-fix-for-tfs-2017-update-3/

Thursday, October 18, 2018

ASP.NET - HTTP Error 500.24 - Internal Server Error

After configuring a new application in IIS and deploying our code to it, the server returned the following error:

HTTP Error 500.24 - Internal Server Error

An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.

Most likely causes: •system.web/identity@impersonate is set to true.

The error message itself pointed us to the 'impersonate’ setting that was causing this error. However for this website we would like to use impersonation, so disabling it was not an option.

Instead what we did was ignoring this error by adding following configuration in our web.config:

<configuration>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
    </system.webServer>
</configuration>

Wednesday, October 17, 2018

DevOps Quick Reference posters

Just a quick tip for today. Willy-Peter Schaub shared a nice set of quick reference posters about Azure and DevOps on Github:

image

One I especially liked is the DevOps approach @ Microsoft:

image

Tuesday, October 16, 2018

ASP.NET Core Configuration - System.ArgumentException: Item has already been added.

In ASP.NET Core you can specify an environment by setting the 'ASPNETCORE_ENVIRONMENT'  environment variable on your server. Yesterday however I wanted to setup a second environment on the same machine. To achieve this I added a second website to IIS and set the environment variable through a parameter in my web.config:

Unfortunately after doing that my application no longer worked. In the Event Viewer I could see the following error message:

Application: dotnet.exe

CoreCLR Version: 4.6.26628.5

Description: The process was terminated due to an unhandled exception.

Exception Info: System.ArgumentException: Item has already been added. Key in dictionary: 'ASPNETCORE_ENVIRONMENT'  Key being added: 'ASPNETCORE_ENVIRONMENT'

   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)

   at System.Environment.ToHashtable(IEnumerable`1 pairs)

   at System.Environment.GetEnvironmentVariables()

   at Microsoft.Extensions.Configuration.EnvironmentVariables.EnvironmentVariablesConfigurationProvider.Load()

   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)

   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()

   at Microsoft.AspNetCore.Hosting.WebHostBuilder..ctor()

   at Web.Program.Main(String[] args) in c:\Program.cs:line 10

It seems that ASP.NET Core doesn’t like that you try to set the same variable twice. Strange because I thought it worked on a previous version of .NET Core(I’m running on 2.1at the moment).

To fix it, I had to remove the environment variable from the system(restart the system) and set the environment for all applications through the web.config.

Anyone with a better solution?

Monday, October 15, 2018

ASP.NET Core–Environment Tag Helper

One of the nice features in ASP.NET Core (MVC) is the introduction of Tag Helpers, a more HTML friendly alternative for HtmlHelpers.

A built-in Tag Helper is the Environment Tag Helper,  it conditionally renders its enclosed content based on the current hosting environment.

In my case I wanted to render some content in the development environment and add some other content in all other environments(test, uat, production). To support this scenario, the Environment Tag Helper gives you include & exclude attributes to control rendering the enclosed content based on the included or excluded hosting environment names.

An example:

In the example above I'm changing the base href for Angular depending on the environment. In development I'm running my angular site under the web application root whereas in other environments I'm running my angular site under a virtual directory.

Tuesday, October 9, 2018

Which OAuth flow should I use?

OAuth 2.0 supports several different grants. By grants we mean ways of retrieving an Access Token. Unfortunately it can be quite a challenge to find out which grant should be used in which situation.

The guys from Auth0 created the following diagram to help you arrive at the correct grant type:

Flowchart for OAuth 2.0 Grants

Monday, October 8, 2018

Async/Await–.ConfigureAwait(false)

On one of my projects, a developer asked my help when he noticed that the application just hang after invoking a specific action on a Web API controller.

In this at first innocent looking piece of code he was using a combination of async and non-async code. Here is a simplified example;

If you ran the code above then you end up with a deadlock. This is what happens:

  1. Thread "A" would be given to the request to run on and "Index" would be called
  2. Thread "A" would call "DoSomethingAsync" and get a Task reference
  3. Thread "A" would then request the ".Result" property of that task and would block until the task completed
  4. The "Task.Delay" call would complete and the runtime would try to continue the "DoSomethingAsync" work
  5. The ASP.NET synchronization context would require that work continue on Thread "A" and so the work would be placed on a queue for Thread "A".

Thread "A". is waiting until the DoSomethingAsync task is completed, but the DoSomethingAsync cannot complete because it needs Thread "A". So we end up with a deadlock situation.

The root cause is that ASP.NET applications have a special synchronization context, that returns to the same thread after an async call complete

    To avoid this kind of issues it’s a good idea to set .ConfigureAwait(false). By doing this you no longer use the ASP.NET synchronization context, meaning that the work can continue on any thread available when the async work has completed.

    Remark: Be aware that this requires your code to be thread-safe.

    As most of the time you don’t care on which thread the work continues, it can be a good idea to always add ".ConfigureAwait(false)" to your await calls. If you don’t want to forget this, you can use the following analyzer in your projects: https://www.nuget.org/packages/ConfigureAwaitChecker.Analyzer. This will give you a warning when a possible ConfigureAwait(false) call is missing. The analyzer is quite smart and detects when a configureawait is possible.

    image

    Remark: When you are using ASP.NET Core, you don’t have to worry about this as the synchronization context that was used for previous versions of ASP.NET is gone.

    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 the end of last year I blogged about the following exception I got when using the WSFederation OWIN middleware together with ADFS.

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

    The problem was related to an incompatibility between the OWIN version(5) and the Microsoft.IdentityModel.Tokens nuget packages. In the meanwhile some newer versions of this package are released, making the issue disappear starting from version 5.2.

    Friday, October 5, 2018

    Introducing Microsoft Learn

    Last month Microsoft launched their new learning website called Microsoft Learn. Goal of Microsoft Learn is to become the one stop for self-paced, guided learning on all of  Microsoft’s platform products and services.

    Today the site already offers more than 80 hours of learning content for Azure, Dynamics 365, PowerApps, Microsoft Flow, and Power BI. Among that content, you’ll find experiences that will help get you ready for new certification exams for developers, administrators, and solution architects.

    A quick tour of the Microsoft Learn experience

    Wednesday, October 3, 2018

    ElasticSearch–More like this

    One of the nice features in ElasticSearch is ‘The More Like This’ Query. ‘The More Like This’ Query finds documents that are "like" a given set of documents. In order to do so, MLT selects a set of representative terms of these input documents, forms a query using these terms, executes the query and returns the results.

    An example:

    The really important parameter is the Like() where you can specify free form text and/or a single or multiple documents.

    More information: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html

    Tuesday, October 2, 2018

    Akka vs Orleans–Comparing Actor systems

    If you are looking for a balanced evaluation and comparison between Akka and Orleans, have a look at https://github.com/akka/akka-meta/blob/master/ComparisonWithOrleans.md.

    Afbeeldingsresultaat voor akka Afbeeldingsresultaat voor microsoft orleans

    The core message is well described in the introduction;

    The most interesting aspect is the difference in primary focus between the two projects:

    • The primary focus of Orleans is to simplify distributed computing and allow non-experts to write efficient, scalable and reliable distributed services.

    • Akka is a toolkit for building distributed systems, offering the full power but also exposing the inherent complexity of this domain.

    Both projects intend to be complete solutions, meaning that Orleans’ second priority is to allow experienced users to control the platform in more detail and adapt it to a wide range of use-cases, while Akka also raises the level of abstraction and offers simplified but very useful abstraction.

    Another difference is that of design methodology:

    • The guiding question for Orleans is “what is the default behavior that is most natural and easy for non-experts?” The second question is then how the expert can make their own decision.

    • Akka’s guiding question is “what is the minimal abstraction that we can provide without compromises?” This means that “good default” for us is not driven by what users might expect, but what we think users will find most useful for reasoning about their program once they have understood the abstraction—familiarity is not a goal in itself.

    I have used both systems in the past and I can stand behind the message as brought forward in this article.

    Monday, October 1, 2018

    IIS–Decryption key specified has invalid hex characters

    After setting a machine key inside my web.config, I got the following IIS error:

    Decryption key specified has invalid hex characters

    Here is the related web.config line:

    <machineKey decryptionKey="decription key is here" validation="SHA1" validationKey="validationkey,IsolateApps" />

    The root cause of this error is that in fact the configuration I specified above is invalid. Using an explicit decryptionKey together with the IsolateApps modifier doesn’t work. The IsolateApps modifier causes ASP.NET to generate a unique key for each application on your server. This is only applicable if you are getting ASP.NET to auto-generate keys at runtime.

    More information: https://stackoverflow.com/questions/15002960/isolateapps-causes-decryption-key-specified-has-invalid-hex-characters