[英]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.