托管asp.core:如何用AddApplicationPart替换过时的RazorViewEngineOptions.CompilationCallback?

时间:2019-01-15 00:57:42

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

RazorViewEngineOptions.CompilationCallback变得过时了...

enter image description here

...,当detailed information provided by MS建议使用ApplicationPartManager.AddApplicationPart而不是CompilationCallback配置程序集时:

  

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

但是,AddApplicationPart仅在我们使用WebHostBuilder(不是从IMvcBuilder派生)以自己的进程启动asp内核时才可应用于IMvcBuilder

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

应如何调用AddApplicationPart?从何处获得IMvcBuilder

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

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

2 个答案:

答案 0 :(得分:0)

当您配置MVC时,可以与Startup中的ApplicationPartManager进行交互。

调用AddMvc()时可以使用

IMvcBuilder。

services.AddMvc() .ConfigureApplicationPartManager(_ => _....)

答案 1 :(得分:0)

是的,我使用

 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 它可以工作,能够对尸体进行欺骗