Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Could not load file or assembly 'netstandard, Version=2.0.0.0, ...'. Reference assemblies should not be loaded for execution

Goal: From a .NET 4.7 console app, using reflection with Assembly.GetType(), I am trying extract the Type of a netstandard 2.0 class from Assembly X. Then I want to create an instance of this Type with Activator.CreateInstance().

What I am trying to do: However, this assembly X has a dependency to netstandard 2.0. To be able to get the Type, netstandard dependency has to be loaded into the AppDomain. That's why when the AppDomain is requesting the netstandard assembly through the AssemblyResolve event, I simply load the dll like this :

var netStandardDllPath = @"C:\Users\xxx\.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll";  return Assembly.LoadFrom(netStandardDllPath); 

Which throws:

System.BadImageFormatException: 'Could not load file or assembly 'file:///C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)'

Inner Exception: BadImageFormatException: Cannot load a reference assembly for execution.

What I know: I know that they want us to load the DLL with Assembly.ReflectionOnlyLoadFrom. But doing that will prevent me from instanciate the type with Activator.CreateInstance(). See Microsoft official post

Also, I tried referencing the Nuget packages NETStandard.Library 2.0.0-preview1-25301-01 and NETStandard.Library.NETFramework 2.0.0-preview1-25305-02 in my console app so it would have the netstandard 2.0 libraries referenced, but it didn't change anything.

Question: Does anyone would know if there is a proper way to load that dll without error, or maybe if this is a bug, or else? Or why this kind of dll is not able to load for execution?

like image 412
Vincent Lerouvillois Avatar asked Jul 10 '17 21:07

Vincent Lerouvillois


People also ask

Where is Netstandard DLL located?

NET standard 2.0 types and members are all packet in a single assemblies netstandard. dll v2. 0 that can be found under C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.

Can not load file or assembly?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

What is Netstandard?

NET Standard allows a library to use more APIs but means it can only be used on more recent versions of . NET. Targeting a lower version reduces the available APIs but means the library can run in more places.

Can you reference .NET core from .NET standard?

NET Core wouldn't be able to reference a . NET Standard project.


2 Answers

The netstandard.dll you are trying to load is a reference assembly that which cannot be loaded for runtime on .NET Framework as pointed out by others. However if you need to resolve that dependency you will need to runtime version that maps to the framework you are trying to run on.

For .NET Standard support we are including them as part of the msbuild extensions that ship with VS so you will want to get the version of netstandard.dll from there. Depending on which version of VS2017 you have installed it should be somewhere like C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll or from the .NET Core 2.0 SDK you can find it C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll

Try using one of those versions in your scenario.

like image 135
Wes Haggard Avatar answered Oct 07 '22 19:10

Wes Haggard


Set Copy Local to true in netstandard.dll properties.

  1. Open Solution Explorer and right click on netstandard.dll.
  2. Set Copy Local to true.

enter image description here

like image 45
Armaan Avatar answered Oct 07 '22 18:10

Armaan