.NET Core vs .NET Framework - Could not load file or assembly 'System.ServiceModel, Reference assemblies should not be loaded for execution

0

I have an ASP.NET Core 3.1 web app.

This application needs to load and execute some code from a 3rd party .NET Framework dll.

Unfortunately, the 3rd party code seems to have a dependency (or something else?) on System.ServiceModel, and when I call the code from that DLL, it throws the following error:

Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)

To try it out in a simpler environment, I have created 2 identical console apps which call this 3rd party component

  • .NET Framework - works OK, no errors thrown
  • .NET Core - same error is thrown

That leads me to believing that something is problematic when I try to call this 3rd party .NET Framework code from .NET Core.

I had a look into a couple of questions, but they are a bit different:
Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0
Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies
Reference assemblies should not be loaded for execution

Any idea what can I do to make it run?

c#
.net
.net-core
asked on Stack Overflow Jan 28, 2021 by Bartosz • edited Jan 29, 2021 by Bilal Mehrban

3 Answers

0

We ran into this issue as well, what worked for us was to make sure that the RID (Runtime Identifier) was set to win-x64 (or win-x32). However our stack was entirely .NET Core/Standard

In your case it is not generally possible to load a .NET Framework dll into the .NET Core runtime as the underlying changes are not backwards compatible. So even if you are able to get it to load you will likely run into runtime exceptions or process crashes/failures.

answered on Stack Overflow Jan 28, 2021 by Menachem Hornbacher
0
  • You cannot load .Net Framework DLL into .NET Core. You can use .NET standard DLL into .NET Core. Use .NET Standard DLL in it. Usually, the third party should provide a .net standard version of the code
answered on Stack Overflow Jan 28, 2021 by Ajinkya Gadgil
0

Actually, it turned out to be quite simple!

I had to add a reference to System.ServiceModel.Primitives nuget in the .NET core application and it started to work all ok!

answered on Stack Overflow Jan 28, 2021 by Bartosz

User contributions licensed under CC BY-SA 3.0