C# 从外部程序集加载标记帮助程序

C# 从外部程序集加载标记帮助程序,c#,asp.net-core,asp.net-core-tag-helpers,C#,Asp.net Core,Asp.net Core Tag Helpers,在带有razor视图的ASP.NET Core 2.0项目中,我正在运行时加载一个包含TagHelpers的程序集 当.dll位于项目的bin文件夹中时,或当taghelpers项目作为依赖项添加到项目中时,标记由taghelpers解析 但是,当程序集加载到bin文件夹之外时,即使程序集加载成功,标记帮助程序也无法工作 当从bin外部的文件夹加载程序集时,如何使标记助手工作 public void ConfigureServices(IServiceCollection services)

在带有razor视图的ASP.NET Core 2.0项目中,我正在运行时加载一个包含TagHelpers的程序集

当.dll位于项目的bin文件夹中时,或当taghelpers项目作为依赖项添加到项目中时,标记由taghelpers解析

但是,当程序集加载到bin文件夹之外时,即使程序集加载成功,标记帮助程序也无法工作

当从bin外部的文件夹加载程序集时,如何使标记助手工作

 public void ConfigureServices(IServiceCollection services)
 {

    var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"D:\SomeTagHelpers\bin\Debug\netcoreapp2.0\SomeTagHelpers.dll");
    var part = new AssemblyPart(asm);
    var builder = services.AddMvc();
    builder.ConfigureApplicationPartManager(appPartManager => appPartManager.ApplicationParts.Add(part));
    builder.AddTagHelpersAsServices();
  }

因此,当在bin文件夹外使用引用时,请使用RazorViewEngineOptions的AdditionalCompilationReferences将引用添加到编译中,以便发现并使用标记帮助程序。此外,不必使用AddTagHelperAsservices()

  public void ConfigureServices(IServiceCollection services)
  {
    var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"D:\SomeTagHelpers\bin\Debug\netcoreapp2.0\SomeTagHelpers.dll");
    var part = new AssemblyPart(asm);
    var builder = services.AddMvc();
    builder.ConfigureApplicationPartManager(appPartManager => appPartManager.ApplicationParts.Add(part));    

    builder.Services.Configure((RazorViewEngineOptions options) =>
    {
      options.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(asm.Location));
    });    
  }