Razor 无法删除动态模板 dll 文件并破坏文件系统
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 贡献者。
从 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 贡献者。