Skip to main content

Posts

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

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,

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: ValueTask AsyncLock Striped Lock TaskEnumerableAwaiter CancelableTaskCompletionSource Extension Methods: ContinueWithSynchronously TryCompleteFromCompletedTask 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

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.

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-pip

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.

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:

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: 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 Remark: You’ll have to wait some time before the updated names show up on the Application map

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: More information: https://blogs.msdn.microsoft.com/devops/2018/09/13/search-msrc-fix-for-tfs-2017-update-3/

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>

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 : One I especially liked is the DevOps approach @ Microsoft:

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()

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.

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:

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: Thread "A" would be given to the request to run on and "Index" would be called Thread "A" would call "DoSomethingAsync" and get a Task reference Thread "A" would then request the ".Result" property of that task and would block until the task completed The "Task.Delay" call would complete and the runtime would try to continue the "DoSomethingAsync" work 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". Threa

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.

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.

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

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 .   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 m

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