In my MVC web application I have a simple static Razor helper in /App_Code/Functions.cshtml that I use to write out HTML for social media sharing on various different view pages:
@helper share (string url = null)
{
if (url != null)
{
string fullUrl = "http://example.com" + url;
<span class="share">
<span class="text">Share</span>
<a href="https://www.facebook.com/sharer.php?u=@fullUrl " class="facebook">Facebook</a>
<a href="https://www.twitter.com/intent/tweet?url=@fullUrl " class="twitter">Twitter</a>
<a href="https://plus.google.com/share?url=@fullUrl " class="google">Google+</a>
</span>
}
}
However, I sometimes get the error “Collection was modified; enumeration operation may not execute” especially when performing a site wide link check for example, and it always breaks on line 5 of the helper where it sets the “fullUrl” variable.
Since I’m not using any collections here, I don’t really know why this is happening or how to fix it. I suspect it could be something to do with thread safety but any help would be greatly appreciated.
This is an example stack trace from the error:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.BrowserLinkExecutionListener.GetOutputPositionTracker(TextWriter textWriter)
at Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.BrowserLinkExecutionListener.BeginContext(PageExecutionContext context)
at CallSite.Target(Closure , CallSite , Object , Object )
at System.Web.WebPages.Instrumentation.PageExecutionListenerAdapter.BeginContext(PageExecutionContextAdapter context)
at System.Web.WebPages.Instrumentation.InstrumentationService.BeginContext(HttpContextBase context, String virtualPath, TextWriter writer, Int32 startPosition, Int32 length, Boolean isLiteral)
at System.Web.WebPages.HelperPage.BeginContext(TextWriter writer, String virtualPath, Int32 startPosition, Int32 length, Boolean isLiteral)
at ASP.Functions.<>c__DisplayClassc.<share>b__b(TextWriter __razor_helper_writer) in \App_Code\Functions.cshtml:line 143
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at ASP._Page_Views_News__Article_cshtml.Execute() in \Views\_Article.cshtml:line 134
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
UPDATE
I've replicated the problem just doing the following...
Helper method in /App_Code/Functions.cshtml:
@helper Test ()
{
@:test
}
Method call at the top of an empty view page:
@Functions.Test()
When the view is requested multiple times in succession as when performing a link check, every so often the same error occurs.
Your code looks innocuous, so consider the stack trace. It might be to do with PageInspector. Here's a possible fix
The core is to add this app setting in web.config
;
<appSettings>
<add key="PageInspector:ServerCodeMappingSupport" value="Disabled" />
</appSettings>
Or add this <remove assembly=
tag in web.config
;
<compilation debug="true" targetFramework="4.5">
<assemblies>
<remove assembly="Microsoft.VisualStudio.Web.PageInspector.Loader,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments