小编Edw*_*ghe的帖子

使用RazorEngine同时解析Razor模板

我在MVC 3 Web应用程序中使用RazorEngine库(http://razorengine.codeplex.com/)来使用Razor模板语言来解析字符串(不是视图).

一般来说,这很好.但是,当多个用户访问同时解析Razor模板的代码时,我偶尔会看到内部Razor编译器中出现的错误(请参阅下面的两个).我在解释这些错误时遇到了麻烦,但我的猜测是我调用Razor编译器的方式不是并发安全的.

这是Razor编译器的已知问题吗?正常的Razor视图(.cshtml)如何不遇到这个问题?有没有一种解决方法比将所有应用程序的调用包含在互斥锁中的Razor.Parse更好?

我的调用代码如下,只是一个简单的包装Razor.Parse:

    protected string ParseTemplate<T>(string templateString, T model)
    {
        //This binderAssembly line is required by NUnit to prevent template compilation errors
        var binderAssembly = typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly;
        var result = Razor.Parse(templateString, model);
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

错误一:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: chunkLength
at System.Text.StringBuilder.ToString()
at System.Web.Razor.Generator.RazorCodeGenerator.BlockContext.MarkEndGeneratedCode()
at System.Web.Razor.Generator.RazorCodeGenerator.WriteBlock(BlockContext block)
at System.Web.Razor.Parser.ParserContext.FlushNextOutputSpan()
at System.Web.Razor.Parser.ParserContext.StartBlock(BlockType blockType, Boolean outputCurrentBufferAsTransition)
at System.Web.Razor.Parser.ParserBase.ParseComment() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc concurrency razor

15
推荐指数
1
解决办法
1万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

concurrency ×1

razor ×1