RazorGeneratorMvcStart throws an System.ArgumentException

Nov 24, 2014 at 10:45 PM
Edited Nov 24, 2014 at 10:46 PM
Hi all,
I followed the steps in the home page and checked the sample project but when I run my application I am getting an exception from RazorGeneratorMvcStart class.

"An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Additional information: An item with the same key has already been added."

This happens because of the line:
var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) {
                UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal
Nov 24, 2014 at 11:52 PM
Do you have a stack?
Nov 25, 2014 at 12:23 AM
Hi David
Thanks a lot for your reply, here is the stack

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer`1 comparer)
at RazorGenerator.Mvc.PrecompiledMvcEngine..ctor(Assembly assembly, String baseVirtualPath, IViewPageActivator viewPageActivator)
at RazorGenerator.Mvc.PrecompiledMvcEngine..ctor(Assembly assembly, String baseVirtualPath)
at RazorGenerator.Mvc.PrecompiledMvcEngine..ctor(Assembly assembly)
at Nicksys.Web.Views.App_Start.RazorGeneratorMvcStart.Start() in e:\NicksysTFS\Core\Web\Main\Source\WebSolution\Nicksys.Web.Views\App_Start\RazorGeneratorMvcStart.cs:line 11
Nov 25, 2014 at 12:37 AM
Sounds like more than one view are getting mapped to the path. Are you using the Vsix? If so, could you verify if there are pages that share the same value for [PageVirtualPath] attribute by looking the designer file? If you're using the MsBuild task, the cs files for the views would be located in your projects obj directory.
Nov 25, 2014 at 1:49 AM
I checked that because I removed most of the views and kept three (Layout and _Viewstart) in the Precompiled project and (Index) in the MVC app. They have different value of PageVirtualPath.
Nov 25, 2014 at 2:00 AM
Unfortunately there isn't much in the PrecompiledMvcViewEngine that allows it to be debugged. What you could do is add this to your startup and see what the results look like:
var result = (from type in assembly.GetTypes()
where typeof(WebPageRenderingBase).IsAssignableFrom(type)
let pageVirtualPath = type.GetCustomAttributes(inherit: false).OfType<PageVirtualPathAttribute>().FirstOrDefault()
where pageVirtualPath != null
select Tuple.Create(pageVirtualPath, type);
This is taken from https://razorgenerator.codeplex.com/SourceControl/latest#RazorGenerator.Mvc/PrecompiledMvcEngine.cs. Perhaps that might tell what views the view engine is discovering.

Just a guess - are you using both the vsix and msbuilld task? That could explain this since they would end up precompiling views with slightly different type names mapping to the same location.
Nov 25, 2014 at 2:24 AM
Great idea, I just found that MSBuild package is already installed, I removed it and delete all the files in the obj folder and now it is working.

Thanks a lot for your great and smart help