[英]Razor View Race Condition
In a custom razor engine, derived from RazorViewEngine, I've spotted what I think is unstable code: 在源自RazorViewEngine的自定义剃刀引擎中,我发现了我认为不稳定的代码:
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
ViewLocationFormats = AddViewDynamicFormat(controllerContext, viewName).ToArray();
AreaViewLocationFormats = AddAreaViewDynamicFormat(controllerContext, viewName).ToArray();
return base.FindView(controllerContext, viewName, masterName, useCache);
}
My concern is that when two requests are being processed "at the same time," the first will set the location formats as it needs, but before it can use them - the second threaded request might have a chance to set the location formats a different way. 我担心的是,当“同时”处理两个请求时,第一个将根据需要设置位置格式,但在它可以使用之前 - 第二个线程请求可能有机会将位置格式设置为不同方式。 The rest gets ugly.
其余的变得丑陋。
Is this a valid concern? 这是一个有效的问题吗? Said differently, does ASP.NET MVC guarantee that the Razor View engine will handle exactly one request at a time?
换句话说,ASP.NET MVC是否保证Razor View引擎一次只能处理一个请求? I doubt that is the case.
我怀疑是这样的。
Likewise, if there was one razor view engine object per request, then this would be ok. 同样,如果每个请求都有一个剃刀视图引擎对象 ,那么这样就可以了。 But I also don't believe that is the case.
但我也不相信是这样的。
UPDATE I have confirmed that this is a race condition. 更新我已经确认这是竞争条件。 I have also found an article showing a dynamic expansive razor search example , which solves the problem without a race condition (though I still wonder if it has a caching issue).
我还发现了一篇文章,展示了一个动态的扩展剃刀搜索示例 ,它解决了没有竞争条件的问题(尽管我仍然怀疑它是否有缓存问题)。
Are ViewLocationFormats
and AreaViewLocationFormats
virtual? ViewLocationFormats
和AreaViewLocationFormats
虚拟的吗? If so, you can put that value in the HttpContext.Current.Items
collection and retrieve it from the overridden properties. 如果是这样,您可以将该值放在
HttpContext.Current.Items
集合中,并从重写的属性中检索它。
I'm nearly positive that the RazorViewEngine isn't meant to be modified at run-time, but merely configured in Global.asax Application_Start. 我几乎肯定RazorViewEngine不是要在运行时修改,而只是在Global.asax Application_Start中配置。 I believe the RazorViewEngine instance exists for the life of the App Domain after startup.
我相信RazorViewEngine实例在启动后存在于App Domain的生命周期中。
If you need to add additional ViewLocationFormats or AreaViewLocationFormats do it in Application_Start. 如果需要添加其他ViewLocationFormats或AreaViewLocationFormats,请在Application_Start中执行。 Application_Start is guaranteed to run only once per App Domain.
保证Application_Start每个App Domain只运行一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.