Why are we receiving TypeIntializer Exception in ServiceStack after upgrading to Core 2.1 and Framework 4.7.2?

3

We have a Visual Studio C# solution with several projects including .NET Standard class libraries, .NET Framework applications (because they use 3rd party references that are written on Framework), and .NET Core applications.

Previously these projects used:

  • .NET Standard 2.0
  • .NET Core 2.0
  • .NET Framework 4.6.1
  • ServiceStack 5.1.0

We had to do something a bit odd. We had .NET Core apps that were hosting ServiceStack services, and we had .NET Framework application that were ServiceStack clients, and both applications were referencing a .NET Standard class library containing Request objects for ServiceStack. Unfortunately the Framework and Core versions of the ServiceStack modules were not compatible, particularly in the Interfaces component. So the Framework application had to reference the ServiceStack.*.Core DLLs (so mythz told us and it solved the problem).

Now we have upgraded our code to

  • .NET Standard 2.0
  • .NET Core 2.2
  • .NET Framework 4.7.2
  • ServiceStack 5.4.0

The code compiles just fine, but when we run it, we cannot instantiate a JsonServiceClient in a Framework application.

  using (var client = new JsonServiceClient(_config.PropagationUrl))
  {
     ...
  }

The first line of the above code throws the following exception:

System.TypeInitializationException HResult=0x80131534 Message=The type initializer for 'ServiceStack.ServiceClientBase' threw an exception. Source=ServiceStack.Client StackTrace: at ServiceStack.ServiceClientBase..ctor() at ServiceStack.JsonServiceClient..ctor(String baseUri) at Stitch.Logic.Coverage.ExtentsActor.PostCellExtentRequest(CellExtentsRequest request) in C:\Work\TMobile\stitch\src\Stitch.Logic\Coverage\Actors\ExtentsActor.cs:line 75

Inner Exception 1: TypeInitializationException: The type initializer for 'ServiceStack.Text.Env' threw an exception.

Inner Exception 2: FileNotFoundException: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Inner Exception 3: FileNotFoundException: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Can anyone tell me why and, more importantly, how to fix this?

c#
.net
.net-core
servicestack
.net-framework-version

1 Answer

2

It's a binding issue, try first either adding or removing the Assembly binding information for System.Runtime.InteropServices.RuntimeInformation:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>

If that doesn't resolve it try removing all the ServiceStack NuGet Packages and assembly bindings, delete the /packages, /bin and /obj folders and add the ServiceStack NuGet packages again.

answered on Stack Overflow Oct 3, 2018 by mythz • edited Oct 3, 2018 by mythz

User contributions licensed under CC BY-SA 3.0