Why does MassTransit.Serialization.SystemTextJsonMessageSerializer 8.0.3 thrown an exception when configuring the bus in .NET 4.6.2 + ASP .NET MVC - asp.net-mvc

We have a .NET Framework 4.6.2 ASP .NET MVC application that configures MassTransit.RabbitMQ 8.0.3 in the Startup class
The code is below
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.UseNewtonsoftJsonSerializer(); //Also tried UseNewtonsoftRawJsonSerializer and without
cfg.Host(WebConfigurationManager.AppSettings["RabbitMQHost"], "/", h =>
{
h.Username(WebConfigurationManager.AppSettings["RabbitMQUsername"]);
h.Password(WebConfigurationManager.AppSettings["RabbitMQPassword"]);
});
});
This is the exception
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'MassTransit.Serialization.SystemTextJsonMessageSerializer' threw an exception.
Source=MassTransit
StackTrace:
at MassTransit.Configuration.SystemTextJsonMessageSerializerFactory.get_ContentType()
at MassTransit.Configuration.SerializationConfiguration.AddSerializer(ISerializerFactory factory, Boolean isSerializer)
at MassTransit.Configuration.SerializationConfiguration.AddSystemTextJson()
at MassTransit.Configuration.EndpointConfiguration..ctor(ITopologyConfiguration topology)
at MassTransit.RabbitMqTransport.Configuration.RabbitMqBusConfiguration..ctor(IRabbitMqTopologyConfiguration topologyConfiguration)
at MassTransit.RabbitMqBusFactory.Create(Action`1 configure)
at Startup.ConfigureMassTransit(IUnityContainer unityContainer) in C:\p\Startup.cs:line 79
at Startup.Configuration(IAppBuilder app) in C:\p\Startup.cs:line 59
Inner Exception 1:
TypeInitializationException: The type initializer for 'System.Text.Encodings.Web.DefaultJavaScriptEncoder' threw an exception.
Inner Exception 2:
TypeInitializationException: The type initializer for 'PerTypeValues`1' threw an exception.
Inner Exception 3:
FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Inner Exception 4:
FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
What I've tried
I read the Serialization section of the MassTransit 8 upgrade guide. I added UseNewtonsoftJsonSerializer. Same error.
I had a look at Microsoft's Migration guide from NewtonSoft.Json to System.Text.Json. Our web app includes both System.Text.Json and NewtonSoft.Json. I tried uninstall System.Text.Json but Masstransit requires that. We use NewtonSoft.Json in a lot of places in our code.
I found some assembly redirects in the .config files. I removed those but I still get the error.
Checked that System.Runtime.CompilerServices.Unsafe (6.0) is installed (as a nuget package)
Questions
Why am I getting this error and how to I fix it?
Other details
One of the errors mentions "DefaultJavaScriptEncoder" and another mentions "SystemTextJsonMessageSerializer".
The Webapp uses Unity 5.6.0 (and Unity.Abstractions 3.2.0, Unity.Container 5.6.0, Unity.MVC 5.0.12, Unity.WebAPI 5.3.0)
Using MassTransit 7.1.8 we do not have these problems.
Update 1
Not adding this as an answer because it's specific to my system.
I managed to fix this by adjusting / fiddling with the dependenyAssembly / bindingRedirect in the web.config file.
I remove System.Runtime.CompilerServices.Unsafe dependencyAssembly / bindingRedirect.
I added these dependencyAssembly / bindingRedirect (which I got from the exception message: change dependencyAssembly, run program, get exception, repeat)
Microsoft.Extensions.Logging.Abstractions
System.Diagnostics.DiagnosticSource
Microsoft.Bcl.AsyncInterfaces
System.Threading.Channels
<!--<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>-->
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.18.0" newVersion="3.1.18.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Channels" publicKeyToken="cc7b13ffcd2ddd51"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>

Related

Could not load file or assembly System.Runtime.CompilerServices.Unsafe on a clean autofac.mv5 installation

I just installed autofac.Mvc5 6.0 on my .net framework 4.7.2 installation.
And without adding any code other than adding the nuget package I get this error:
Could not load file or assembly
System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a or one of it's
dependences. The found Assembly's manifest definition does not match
the Assembly reference. (Exception from HRESULT: 0x80131040)
Anyone got any idea?
You probably have another library that uses a different version of System.Runtime.CompilerServices.Unsafe. What's in web.config?
Did you tried bindingRedirect?
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
If you have a newer version replace it in the newVersion above.

TypeLoadException: From C# code that attempts to interop with F# code

I am struggling to resolve this runtime exception from my C# PCL that references an F# PCL:
Unhandled Exception:
System.TypeLoadException: Could not resolve type with token 0100001d
(from typeref, class/assembly Microsoft.FSharp.Core.FSharpResult`2,
FSharp.Core, Version=3.259.41.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a) occurred
NOTE:
I have ensured that my C# and F# project reference the same FSharp.Core version.
I remember having to add an XML file for versioning whenever I my test projects in F# wouldn't play nice with my F# projects. Is this the same case?
I added the following to my app config on my Xamarin.Forms PCL:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-999.999.999.999" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
But I still receive the same error.
FSharpResult is (I assume) the Result<'TSuccess, 'TError> type that was introduced in FSharp 4.1 so you should get FSharp.Core Version 4.4.1.0.

FsUnit: Unable to test portable library due to it and test project having different F#.Core versions

I have a Portable Library, for which the FSharp.Core version is 3.7.4.0. Installing (in the Unit Test project) FsUnit installs, as a dependency, FSharp.Core version 3.1.2.5.
Due to this, using the portable library's functions in my Unit Test project, for example:
module StammaTests.PieceTests
open Stamma
open NUnit.Framework
open FsUnitTyped
[<Test>]
let ``Testing a Basic function`` () =
Piece.toChar Black King |> shouldEqual 'k'
yields error:
Result Message: System.IO.FileLoadException : Could not load file or assembly 'FSharp.Core, Version=3.7.4.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
Tried updating the FSharp.Core version from NuGet to 4.0.0.1 (even checking both projects when updating) and now even something simple like :
[<Test>]
let ``Testing the test`` () = 1 |> shouldEqual 1
doesn't work, giving this similar error.
Result Message: System.IO.FileLoadException : Could not load file or
assembly 'FSharp.Core, Version=4.3.1.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
And the error for the first failing test doesn't change.
I feel like I am missing something painfully obvious, and I found several people with similar problems, but I don't understand what they did to solve it (they all seem to have solved it ..) For example this one.
Edit
Both projects are libraries and I do not have an app.config file to add anything to.
Add a binding redirect in your app.config file to redirect all FSharp.Core bindings to your desired version. For example, to use version 4.4.0, your app.config file would look something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I found a solution that actually worked here
Basically, adding an App.config to the test project, and writing the following:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.4.14350" newVersion="2.6.4.14350" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
It adds binding to both Fsharp.Core and NUnit.Framework, unlike the usual solutions where you only add a binding for Fsharp.Core.

how to use DotNetOpenAuth.Core, Version=4.3.0.0 with vs 2012

i'm using visual studio (2012 or 2013) with asp mvc 4
and it gives me the following error :
Could not load file or assembly 'DotNetOpenAuth.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
i need to use version 4.3.0.0 i installed it using nuget
I know I had problems with the System.Mvc dependency being of the wrong version. Adding the following assembly "rebinding" to your web.config solved the problem (note the version numbers):
<configuration>
<runtime>
<!-- When targeting ASP.NET MVC 3-4, this assemblyBinding makes MVC 1 and 2 references relink
to MVC 3-4 so libraries such as DotNetOpenAuth that compile against MVC 1 will work with it. -->
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Sidenote: I discovered DotNetOpenAuth.Ultimate that bundles everything into one single DLL! It's much much simpler to maintain that the default DotNetOpenAuth and its huge number of packages....

NuGet adding rogue line to web.config MVC4

Every time I install ANYTHING using Nuget, this gets added to my <Runtime><asseblyBinding> section in my web.config:
<dependentAssembly>
<assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
Obviously I'm using MVC4 so references to Razor 2.0.0.0 breaks things, and the following runtime error is thrown.
Could not load type 'System.Web.Razor.Parser.SyntaxTree.CodeSpan' from assembly 'System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Its easy to fix, just remove the rouge section from Web.Config, but very annoying all the same! Anyone know why this might be happening, or how to fix this?
I'm using Nuget Package Manager Version 1.7.30402.9028
Individual packages are able to provide installation routines written in PowerShell, which is probably what's happening here. Does the same thing happen even if you install something unrelated to Razor, like, say, JQuery?
Thanks,
Clay

Resources