Tuesday, March 31, 2020

ElasticSearch - Error when using latest OpenJRE

While migration from ElasticSearch 5 to 7.6.1 I had to take an extra step and first migrate to ElasticSearch 6.8.

When I tried to run the 6.8 version, it failed with the following error message:

2020-03-30 10:18:32 Commons Daemon procrun stderr initialized

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessClassInPackage.jdk.internal.vm.annotation")

               at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)

               at java.base/java.security.AccessController.checkPermission(AccessController.java:1036)

               at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:408)

               at java.base/java.lang.SecurityManager.checkPackageAccess(SecurityManager.java:1324)

               at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:174)

               at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

               at java.base/java.lang.Class.forName0(Native Method)

               at java.base/java.lang.Class.forName(Class.java:416)

               at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)

               at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)

               at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)

               at java.base/sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:440)

               at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:242)

               at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:121)

               at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:73)

               at java.base/java.lang.reflect.Executable.declaredAnnotations(Executable.java:614)

               at java.base/java.lang.reflect.Executable.declaredAnnotations(Executable.java:612)

               at java.base/java.lang.reflect.Executable.getAnnotation(Executable.java:582)

               at java.base/java.lang.reflect.Method.getAnnotation(Method.java:696)

               at <<<guice>>>

               at org.elasticsearch.repositories.RepositoriesModule.lambda$configure$1(RepositoriesModule.java:68)

               at java.base/java.util.HashMap.forEach(HashMap.java:1338)

               at java.base/java.util.Collections$UnmodifiableMap.forEach(Collections.java:1505)

               at org.elasticsearch.repositories.RepositoriesModule.configure(RepositoriesModule.java:68)

               at <<<guice>>>

               at org.elasticsearch.node.Node.<init>(Node.java:492)

               at org.elasticsearch.node.Node.<init>(Node.java:245)

at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:212)

               at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:212)

               at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:322)

               <<<truncated>>>

The problem was that I already installed the latest JAVA OpenJRE. This version is not compatible with ElasticSearch 6.8.

To fix it I had to downgrade to Java 12.

Monday, March 30, 2020

ElasticSearch–Failed to run ElasticSearch 7.6.1 after upgrade

After upgrading ElasticSearch to 7.6.1, it failed to start when I tried to run ElasticSearch as a windows service.

In the logs I found the following error message:

[2020-03-30 10:04:11] [info]  [ 4516] Service 'elasticsearch-service-x64' installed

[2020-03-30 10:04:11] [info]  [ 4516] Commons Daemon procrun finished

[2020-03-30 10:04:23] [info]  [ 4336] Commons Daemon procrun (1.1.0.0 64-bit) started

[2020-03-30 10:04:23] [info]  [ 4336] Running 'elasticsearch-service-x64' Service...

[2020-03-30 10:04:23] [info]  [ 4992] Starting service...

[2020-03-30 10:04:23] [error] [ 2432] CreateJavaVM Failed

[2020-03-30 10:04:23] [error] [ 2432] The system cannot find the file specified.

[2020-03-30 10:04:23] [error] [ 4992] Failed to start Java

[2020-03-30 10:04:23] [error] [ 4992] ServiceStart returned 4

[2020-03-30 10:04:23] [info]  [ 4336] Run service finished.

[2020-03-30 10:04:23] [info]  [ 4336] Commons Daemon procrun finished

The problem is that ElasticSearch tries to pass on a ‘-server’ option which is no longer supported in the latest JAVA versions.

To fix I had to change the Java Options:

  • Install the windows service through ‘elasticsearch-service install’
  • Open the ElasticSearch service manager through ‘elasticsearch-service manager’
  • Go to the Java tab and remove the ‘-server’ flag from the Java Options

  • Start the service

Friday, March 27, 2020

C#–Discards

Have a look at the code below:

Did you notice the underscore character ‘_’? This is the discard character.

From the documentation:

Starting with C# 7.0, C# supports discards, which are temporary, dummy variables that are intentionally unused in application code. Discards are equivalent to unassigned variables; they do not have a value. Because there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations. Because they make the intent of your code clear, they enhance its readability and maintainability.

This is more than just a placeholder variable. If you try to use the assigned _ value later on in your code, this will fail:

Thursday, March 26, 2020

Microsoft Orleans - Multi silo deployment behind a load balancer

In one of my projects we are using Orleans, the virtual actor framework from Microsoft. Your Orleans backend(the cluster) can span out over multiple servers(silos).

In a cluster there are 2 types of communication  happening:

  • silo-to-silo communication(through the silo port)
  • client-to-silo communication(through the gateway port)

As a result on every server 2 ports should be opened to enable this traffic.

Load balancing

The Orleans runtime does all the load balancing for you. More important it is one of the pillars of the Orleans runtime. The runtime tries to make everything balanced, since balancing allows to maximize resource usage and avoid hotspots, which leads to better performance, as well as helps with elasticity.

More information: https://dotnet.github.io/orleans/Documentation/implementation/load_balancing.html

This means that it doesn’t make sense to put a load balancer between the clients and the cluster as this would defy one of the core purposes of the Orleans runtime.

But what if a load balancer exists?

This was exactly the situation I had with one of my customers. They were not fully aware that it doesn’t make sense to add a load balancer and installed our Orleans application on 2 servers behind a load balancer.

What they noticed was the following? As long as only one silo was running everything worked as expected. However the moment a second silo was spinned up, the other one shut down.

The problem was that due to the load balancer both silo got the same IP address. This was easily detectable in the Orleans Membershiptable:

To solve it, we explicitly specified the IP address of each server in the silo configuration:

Wednesday, March 25, 2020

Autofac - Decorators

One of the nice features of Autofac is the support for decorators.

A decorator class implements the same interface as the class it “wraps”.  With a decorator you can add functionality to a class without changing it’s signature.

Let’s walk through an example step by step:

Here is the class and it’s corresponding interface I want to decorate:

Nothing special at the Autofac level yet:

Let’s now extend the behavior of this class through a decorator. What if we want to benchmark the execution time of the LogMessage() method? The decorator class should implement the same interface AND inject the interface at the same time:

Now we need to register the decorator in Autofac:

Remark: When using generics, it is also possible to register an open generic  decorator. Therefore you need to use the RegisterGenericDecorator() overload

More information: https://autofaccn.readthedocs.io/en/latest/advanced/adapters-decorators.html

Tuesday, March 24, 2020

C#–is null or == null that’s the question

While reviewing some code I noticed the following code construct:

So far I’ve always written my null checks using the following syntax:

I wasn’t aware that this was even possible. Turns out this was introduced in C# 7.

Why would you use the ‘is’ keyword instead of ‘==’? The ‘is’ keyword ignores any operator overloads so you don’t end up with unexpected behavior when you are using operator overloading.

Learned something today? Check!

Monday, March 23, 2020

Free SQL Server training during the quarantines

Brent Ozar did a nice gesture in these difficult times and gave away part of his training course for free. So if you really need to spice up your SQL server skills, this is really the training for you.

Start with the ‘How to Think Like the Engine’:

Then continue with the ‘Fundamentals of Index tuning’:

And if you want to go even deeper, take it to the next level with ‘Fundamentals of Query tuning’:

Thanks Brent for all the great (free) content!

        Friday, March 20, 2020

        Autofac–Open Generics

        After registering an open generic type in Autofac, I got the following error when running the application:

        Here is the code I was using to register my MediatR behavior in Autofac:

        Turns out that if you are using open generics you have to use a different method on the Autofac container builder:

        More information in the Autofac documentation: https://autofaccn.readthedocs.io/en/latest/register/registration.html#open-generic-components

        Thursday, March 19, 2020

        Pluralsight–Free conference content

        Yesterday Pluralsight announced a new platform experience: Conferences.

        Through conferences breakout sessions from some of the world’s best technology conferences are made accessible anytime, anywhere.

        As a gesture to the world Pluralsight made all Conference content free to everyone through July 1, 2020—even if you don’t have a current Pluralsight subscription.

        Wednesday, March 18, 2020

        WinRM - Testing your WinRM connection

        While configuring a new server, I had to activate WinRM(Windows Remote Management).

        This is quite easy thanks to the winrm quickconfig command. This command will do the following:

        • Starts the WinRM service, and sets the service startup type to auto-start.
        • Configures a listener for the ports that send and receive WS-Management protocol messages using either HTTP or HTTPS on any IP address.
        • Defines ICF exceptions for the WinRM service, and opens the ports for HTTP and HTTPS.

        To test the WinRM connection from another server, you can use the test-wsman command:

        PS C:\Users\bawu> test-wsman -computername tstsrv30

        If everything is OK, you’ll get the following answer back:

        wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd

        ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd

        ProductVendor   : Microsoft Corporation

        ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

        Tuesday, March 17, 2020

        ASP.NET Core–Use Dependency Injection in Authentication middleware events

        Most of the authentication middleware in ASP.NET Core follows the same pattern; it exposes an options object where you can register for multiple events:

        But what if you want to use dependency injection inside an event handler? The moment the middleware is configured the IoC container is still in construction and is not accessible yet.

        What you can do is, instead of directly registering an event handler for a specific event is to specify an event class using the EventsType property:

        Monday, March 16, 2020

        Orleans - System.TypeAccessException: Named type "Messages.Example" is invalid: Type string "Message.Example" cannot be resolved.

        While debugging an Orleans application, the following exception was thrown:

        System.TypeAccessException: Named type "Messages.Example" is invalid: Type string "Messages.Example" cannot be resolved. at Orleans.Serialization.BinaryTokenStreamReader.ReadSpecifiedTypeHeader() at Orleans.Serialization.SerializationManager.DeserializeInner(Type expected, BinaryTokenStreamReader stream) at Orleans.Serialization.BuiltInTypes.DeserializeOrleansResponse(Type expected, BinaryTokenStreamReader stream) at Orleans.Serialization.SerializationManager.DeserializeInner(Type expected, BinaryTokenStreamReader stream) at Orleans.Serialization.SerializationManager.Deserialize(Type t, BinaryTokenStreamReader stream) at Orleans.Serialization.SerializationManager.Deserialize(BinaryTokenStreamReader stream) at Orleans.Runtime.Message.DeserializeBody(List`1 bytes) at Orleans.Runtime.Message.get_BodyObject() at Orleans.Runtime.GrainReference.ResponseCallback(Message message, TaskCompletionSource`1 context) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Orleans.Runtime.GrainReference.d__42`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

        The reason is that Orleans is unable to find the serialized object. To fix it I had to add the following line to the Orleans client configuration:

        .ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(Messages.Example).Assembly)

        Here is the full config:

        Friday, March 13, 2020

        Building careers with empathy

        Let’s end the week with an inspirational video. Scott Hanselman shares tips how to be successful as programmer and program manager.

        If there is one thing I remember from this video; “Knowing one layer deeper in the stack than your neighbors.”

        Thursday, March 12, 2020

        Multiple inheritance in C#

        One of my interview questions I dare to ask is “Can you do multiple inheritance in C#?”.

        So far, the correct answer always was ‘No’, but with the release of C# 8 the answer became more nuanced. Although a C# class can implement multiple interfaces it can inherit from only one base class.  Until C# 8, only the base class could provide code that is usable by the derived class.

        With C# 8, interfaces can also provide code to their implementing classes. This allows us to share code without a common base class. Finally we can do a (kind of) multiple inheritance…

        Wednesday, March 11, 2020

        ASP.NET Core–Tempdata Peek() and Keep()

        Last week I blogged about the usage of TempData in ASP.NET Core. By default the data is stored until it's read in another request.

        After you have read the data through the indexer the data will be removed from the TempData dictionary:

        But what if you want to keep(no pun intended) the data around longer? This is possible through the usage of the Peek() and Keep() methods. With the Peek() method you can read the data without deleting it. With the Keep() method you can mark the data for retention.

        Nice trick!

        Tuesday, March 10, 2020

        Identity Server 4 - Angular–Chrome’s samesite cookie changes

        Today I got into trouble when I tried to run an Ionic(Angular) application we had build. After being redirect to IdentityServer and returned to my application after  a successful login, a few seconds later I got the message that my session was expired and that I had to login again(and again, and again, …).

        Inside our application we are using the great angular-oauth2-oidc library. One of it’s nice features is that it keeps our identity and access tokens up-to-date thanks to a built-in silent refresh mechanism. This mechanism uses a hidden iframe to call IdentityServer to renew our tokens.

        Remark: we need this hidden iframe hack as we are still using an Implicit Flow. The plan is to switch to Code Flow which allows us to use refresh tokens.

        Inside the developer console I see the following warnings:

        A cookie associated with a cross-site resource at https://ids.development/ was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

        It is the refresh that fails inside the iframe. The angular-oauth2-oidc library returned the following messages:

        angular-oauth2-oidc.js:757 sessionCheckEventListener

        angular-oauth2-oidc.js:757 sessionCheckEventListener wrong origin https://localhost expected https://ids.development

        angular-oauth2-oidc.js:757 got info from session check inframe MessageEvent {isTrusted: true, data: "#error=login_required&state=pBN472WnAzQQ_IGcGSqpyq3-5mPtT-si9XSclJPpLirjm", origin: "https://localhost", lastEventId: "", source: Window, …}

        angular-oauth2-oidc.js:757 parsed url {error: "login_required", state: "pBN472WnAzQQ_IGcGSqpyq3-5mPtT-si9XSclJPpLirjm"}

        angular-oauth2-oidc.js:757 error trying to login

        angular-oauth2-oidc.js:757 silent refresh did not work after session changed

        angular-oauth2-oidc.js:757 silent refresh failed after session changed

        To fix it, we have to do a change in IdentityServer and configure a different cookie policy:

        More information: https://www.thinktecture.com/en/identity/samesite/prepare-your-identityserver/

        Monday, March 9, 2020

        XUnit - Assert.Collection

        A colleague asked me to take a look at the following code inside a test project:

        My first guess would be that this code checks that the specified condition(the contains) is true for every element in the list. 

        This turns out not to be the case. The Assert.Collection expects a list of element inspectors, one for every item in the list. The first inspector is used to check the first item, the second inspector the second item and so on. The number of inspectors should match the number of elements in the list.

        An example:

        The behavior I expected could be achieved using the Assert.All method:

        Friday, March 6, 2020

        ASP.NET Core MVC - Use of partial may result in deadlocks

        I’m currently migrating an existing ASP.NET MVC application to ASP.NET Core.

        When copying over a view with a Partial view in it, I got the following warning:

        Although the Partial method still exists, it is recommended to switch to the asynchronous version as the synchronous version can result in deadlocks in certain scenarios.

        The solution is simple; replace calls to @Html.Partial with @await Html.PartialAsync or the Partial Tag Helper.

        Thursday, March 5, 2020

        ASP.NET Core–Using TempData results in a 500 error

        I’m currently migrating an existing ASP.NET MVC application to ASP.NET Core. To share data between multiple page requests I’m trying to use the TempData. TempData stores your data until it's read in another request.
        This is how my code looked like:

        Unfortunately this resulted in a 500 error without any further explanation 😒.
        What am I doing wrong? The thing is that you cannot store a complex object inside the TempData directly. Instead of returning a meaningful error you get only a 500 message.
        To fix it, I added some serialization logic before storing the object in the TempData dictionary:

        Wednesday, March 4, 2020

        Application Insights Agent

        The Application Insights Agent replaces the old Status Monitor tool. It allows you to monitor .NET web applications hosted in IIS without the need to change your code.

        You can install the agent from the Powershell gallery: https://www.powershellgallery.com/packages/Az.ApplicationMonitor

        Installation steps

        • First you need to install/update PowershellGet. Therefore open an elevated Powershell prompt and execute the following commands:
          • Install-PackageProvider -Name NuGet –Force
          • Install-Module -Name PowerShellGet –Force
          • Update-Module -Name PowerShellGet
        • Next, close and re-open the elevated Powershell prompt. If you forget to do that you’ll probably get the following error when you try to install the Az.ApplicationMonitor module:
          • WARNING: The specified module 'Az.ApplicationMonitor' with PowerShellGetFormatVersion '2.0' is not supported by the current version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'Az.ApplicationMonitor'.

        • In a new command prompt execute the following command:
          • Install-Module -Name Az.ApplicationMonitor -RequiredVersion 1.0.1
          • Hit [Y]es to ignore the untrusted repository warning and accept the license agreement
        • Check that the installation was done succesfully by executing the following command:
          • Enable-ApplicationInsightsMonitoring

        Tuesday, March 3, 2020

        ASP.NET Core - Kendo UI–Grid remains empty

        I’m currently migrating an existing ASP.NET MVC application to ASP.NET Core. After 3 years of working on Single Page Applications through Angular returning to MVC was as straightforward as I hoped.

        As long as you stay in the full page refresh cycle, everything is quite easy but the moment you want to sprinkle some AJAX magic on top of your code, it turns into a mess quite fast.

        But not all is bad. Thanks to control libraries like Kendo UI you get a lot of functionality with almost no code.

        For example the Kendo Grid control is pure magic! Unfortunately it didn’t work out-of-the-box. There is 1 important extra configuration step required. You need to tell ASP.NET Core to maintain the property name casing:

        Monday, March 2, 2020

        Shape Up: Stop Running in Circles and Ship Work that Matters

        I’m always looking at ways to broader my horizon and find new ways to build and ship software.

        An interesting approach is the one used by Basecamp. They have bundled their way of working in a free ebook:

        Shape Up: Stop Running in Circles and Ship Work that Matters

        Shape Up is for product development teams who struggle to ship. Full of eye-opening insights, Shape Up will help you break free of "best practices" that aren't working, think deeper about the right problems, and start shipping meaningful projects.