I'm trying to load application parts into an ASP.NET Core 2.0 MVC appication by using the System.Runtime.Loader
package but it won't let me.
The error message says:
FileNotFoundException: Could not load file or assembly 'System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
In the Startup.cs
I use it like this (it should just be proof of concept that this works so I can move it into a dedicated class or an extension):
services
.AddMvc()
.ConfigureApplicationPartManager(apm =>
{
var parts = Directory.GetFiles(_rootPath, "*.Part.*");
foreach (var part in parts)
{
apm.ApplicationParts.Add(new AssemblyPart(AssemblyLoadContext.Default.LoadFromAssemblyPath(part)));
}
});
The package I installed has the version 4.3.0
and I'm using it in an ASP.NET Core 2.0 with .NET 4.6.2 project.
I thought maybe assembly-binding would help so I added a runtime
configuration to the app.config
<runtime>
<gcServer enabled="true"/>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<publisherPolicy apply="no"/>
<assemblyIdentity name="System.Runtime.Loader" publicKeyToken="b03f5f7f11d50a3a" culture="en-US"/>
<bindingRedirect oldVersion="4.3.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
but this didn't help. I also specified a range like 1.0.0.0-5.0.0.0
but this didn't work either.
The stack trace that IE ouputs is this:
WebApiProject.Startup.<ConfigureServices>b__8_1(ApplicationPartManager apm) Microsoft.Extensions.DependencyInjection.MvcCoreMvcBuilderExtensions.ConfigureApplicationPartManager(IMvcBuilder builder, Action<ApplicationPartManager> setupAction) WebApiProject.Startup.ConfigureServices(IServiceCollection services) in Startup.cs - 38. public IHostingEnvironment Environment { get; } 39. 40. // This method gets called by the runtime. Use this method to add services to the container. 41. public void ConfigureServices(IServiceCollection services) 42. { 43. // Add framework services. 44. services 45. .AddMvc() ... 50. .ConfigureApplicationPartManager(apm => System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceColection services) Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
In the output folder there are a lot of different System.Runtime.*
files but none of them is named System.Runtime.Loader
. Maybe this is the problem but where is this file? I searched the packages but there is no such .dll
so why is VS trying to load it?
Oh, great. I found the package. NuGet did not install it as usual into the packages
folder of the soulution but instead it was here:
c:\Users\%USER%\.nuget\packages\system.runtime.loader\4.3.0\lib\
Sadly there is no version net462
. The folder contains an empty file named: _
There are couple of other folders but there is only one .dll
there:
MonoAndroid10\
MonoTouch10\
net462\
netstandard1.5\ <-- System.Runtime.Loader.dll
xamarinios10\
xamarinmac20\
xamarintvos10\
xamarinwatchos10\
Why does this even install? How is it possible that I can use the types in code but the *.dll
fails to load even if I copy it to the output folder? Is this even supposed to work in a project that mixes .net core and the old framework?
Is there anything else I can do?
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.
This error usually means that the assembly was not found. Try verifying that the file exists in the directory where your application is running.
“An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to the 64-bit build of an application being deployed to IIS, which is being run in 32-bits.
See https://github.com/dotnet/corefx/issues/22142
The TLDR is that System.Runtime.Loader is not supported for .Net Framework
You will have to use Assembly.Load() instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With