[英]Razor fails to delete dynamic template dll files and trashes filesystem
从Razor模板引擎3.3.0升级到3.6.1后,我遇到了预编译模板的问题 - 即使是在页面上给出的琐碎样本,也会发生这样的事情:
using System;
using RazorEngine;
using RazorEngine.Templating;
using System.Diagnostics;
namespace RazorTest
{
class Program
{
static void Main(string[] args)
{
string template = "Hello @Model.Name, welcome to RazorEngine!";
Debug.WriteLine("Before Compile()");
var result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });
Debug.WriteLine("After Compile()");
}
}
}
尝试删除生成的dll文件时,在退出时抛出System.UnauthorizedAccessException
。 调试输出很好地显示了一切:
Before Compile()
'RazorTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\user\Documents\Visual Studio 2010\Projects\RazorTest\RazorTest\bin\Debug\System.Web.Razor.dll'
'RazorTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\user\AppData\Local\Temp\RazorEngine_zzxr14ak.ysb\CompiledRazorTemplates.Dynamic.RazorEngine_dc2066212315402592a6d2d155476c19.dll'
'RazorTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'Anonymously Hosted DynamicMethods Assembly'
'RazorTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll'
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
After Compile()
The thread 'vshost.RunParkingWindow' (0x3064) has exited with code 0 (0x0).
The thread '<No Name>' (0x2df0) has exited with code 0 (0x0).
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
The program '[26908] RazorTest.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
应用程序在编译期间加载了dll文件,因此如果没有进行某种卸载,Razor将无法删除它,并且文件将保留在磁盘上。
即使模型的类型被赋予Razor认为模板是动态的(至少从dll名称判断),还有什么看起来很奇怪。
有没有更多经验的Razor遇到过这个或者可以提供一些如何克服这个问题的提示?
临时文件存在问题,已在3.6.4(大部分)中修复。 如果您需要详细信息,请阅读https://github.com/Antaris/RazorEngine/issues/244 。 仍然first chance exceptions of type 'System.UnauthorizedAccessException'
,但它们由RazorEngine内部处理。
实际上,您的模板使用dyanmic
作为模型类型进行编译,因为您已将null = dynamic
作为modeltype
参数。
如果要使用静态类型编译模板
Engine.Razor.RunCompile(template, "templateKey", typeof(MyModel), new MyModel());
我们将类型显式化的原因是,您现在可以通过指定公共基类型或显式使用null = dynamic
来为多个类型重用相同的模板:
// Will compile only once
Engine.Razor.RunCompile(template, "templateKey", typeof(MyBaseModel), new MyModel1());
Engine.Razor.RunCompile(template, "templateKey", typeof(MyBaseModel), new MyModel2());
// Will start a new compilation, and load another assembly
Engine.Razor.RunCompile(template, "templateKey", typeof(MyModel3), new MyModel3());
只要MyModel1
和MyMode2
继承自MyBaseModel
。 或者您可以使用动态:
// Will compile only once
Engine.Razor.RunCompile(template, "templateKey", null, new FirstModel());
Engine.Razor.RunCompile(template, "templateKey", null, new SecondModel());
请注意,使用动态模型甚至不需要从相同的基类型继承。 只要FirstModel
和SecondModel
具有模板所需的所有属性,它就可以工作(但它不会在模板编译时失败,而是在模板运行时失败)。
这对包含和布局模板特别有用(现在可以更加自定义)。
希望这可以帮助。 matthid,RazorEngine的贡献者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.