asp.net-core - 托管 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));
...
解决方案
是的,我使用
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 并且它可以工作,能够加载程序集
推荐阅读
- asp.net-core - 当 EF DbContext 执行选择数据查询时,Blazor Server 获取当前登录的用户
- amazon-web-services - 寻找 aws CloudWatch Insight 选项以仅查看或记录拒绝原因
- python - Python Kivy/Pyjnius android NotificationListenerService
- asp.net-core - 在 Azure DevOps 中实现权限和组
- certificate - 证书链 PKI 中不存在 ADCS 根 CA
- sql - 从具有开始和结束日期的记录列表中获取每个工作日的平均持续时间
- c++ - 停止 If 语句一次运行整个代码
- c++ - 就地对线性数据结构进行排序
- python - 如何在访问元素的同时遍历二维列表
- android - 尝试在 android 11 上获取网络功能时出现安全异常