Could not load file or assembly 'System.Runtime.Loader' for adding application parts into MVC

只愿长相守 提交于 2020-01-14 08:00:54

问题


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?


Update

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?


回答1:


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



来源:https://stackoverflow.com/questions/45741039/could-not-load-file-or-assembly-system-runtime-loader-for-adding-application-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!