从外部程序集加载 TagHelper

Loading TagHelpers from an external assembly

在 ASP.NET 具有剃刀视图的核心 2.0 项目中,我正在 运行 加载一个程序集,其中包含 TagHelpers。

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

但是,当程序集加载到 bin 文件夹之外时,即使程序集加载成功,TagHelpers 也不起作用。

当从 bin 外的文件夹加载程序集时,如何让 TagHelpers 工作?

 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 将引用添加到编译中,以便发现和工作标签助手。此外,没有必要使用 AddTagHelpersAsServices()。

  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));
    });    
  }