Razor无法删除动态模板DLL文件并删除文件系统

时间:2015-03-05 13:02:47

标签: c# 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遇到过这个或者可以提供一些如何克服这个问题的提示?

1 个答案:

答案 0 :(得分:6)

临时文件存在问题,已在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());

只要MyModel1MyMode2继承自MyBaseModel,这就有效。或者你可以使用动态:

// Will compile only once
Engine.Razor.RunCompile(template, "templateKey", null, new FirstModel());
Engine.Razor.RunCompile(template, "templateKey", null, new SecondModel());

请注意,使用动态模型甚至不需要从相同的基本类型继承。只要FirstModelSecondModel拥有模板所需的所有属性,它就会起作用(但是在模板编译时却不会在模板运行时失败)。

这对包含和布局模板特别有用(现在可以更加自定义)。

希望这会有所帮助。 matthid,RazorEngine的贡献者。

相关问题