首页 > 解决方案 > 托管 asp.core:如何用 AddApplicationPart 替换过时的 RazorViewEngineOptions.CompilationCallback?

问题描述

RazorViewEngineOptions.CompilationCallback 已过时...

在此处输入图像描述

...当MS 提供的详细信息ApplicationPartManager.AddApplicationPart建议使用而不是 CompilationCallback配置程序集时:

使用这些 API 将程序集引用添加到编译上下文以进行运行时编译的应用程序应改为使用 ApplicationPartManager.AddApplicationPart 为每个程序集引用添加应用程序部件

但是AddApplicationPart在我们使用的自己的进程中启动asp核心时只能应用于IMvcBuilder WebHostBuilder(它不是从IMvcBuilder派生的)

var hostBuilder = new WebHostBuilder()
    // contains obsolete code
    //.ConfigureServices(TestManager.InitializeServices) 
    .AddApplicationPart(..); // compilation error, impossible to apply, IMvcBuilder expected !

AddApplicationPart应该怎么称呼?在哪里获得IMvcBuilder

以前使用的代码(为 asp 服务器收集程序集)应替换为AddApplicationPart

public static void InitializeServices(IServiceCollection services){
  services.Configure((RazorViewEngineOptions options) =>
  {
     var previous = options.CompilationCallback;
     options.CompilationCallback = (context) =>
     {
        previous?.Invoke(context);

        var assembly = typeof(Startup).GetTypeInfo().Assembly;
        var assemblies = assembly.GetReferencedAssemblies().Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location))
                .ToList();
        assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location));
        ...

标签: asp.net-coreasp.net-core-2.2

解决方案


是的,我使用

 services.AddMvc()
            .ConfigureApplicationPartManager(context =>
            {
                var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
                var assemblies = startupAssembly.GetReferencedAssemblies().Select(Assembly.Load).ToList();
                assemblies.Add(Assembly.Load(new AssemblyName("netstandard")));
                assemblies.Add(Assembly.Load(new AssemblyName("mscorlib")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.Private.Corelib")));
                assemblies.Add(Assembly.Load(new AssemblyName("System")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.IO")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.Linq")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.Threading.Tasks")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.Runtime")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.Dynamic.Runtime")));
                assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor.Runtime")));
                assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc")));
                assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")));
                assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc.Razor")));
                assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Html.Abstractions")));
                assemblies.Add(Assembly.Load(new AssemblyName("System.Text.Encodings.Web")));
                foreach (var assembly in assemblies)
                {
                    context.ApplicationParts.Add(new AssemblyPart(assembly));
                }
            })

替换 Compilationcallback 并且它可以工作,能够加载程序集


推荐阅读