TL;DR
If razor intellisense stops working suddenly, try deleting your C:\Users\username\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
folder.
The Problem
When I was opening Visual Studio the other day, I was greeted with the following error message:
Closing and reopening Visual Studio didn't fix the error - it occurred every time I opened a razor view (.cshtml) page. While Visual Studio itself did not crash, highlightning and intellisense was broken inside the view. E.g. razor comments
were not highlighted correctly (just as in this post) and my favourite spellchecker (intellisense) had stopped working as well.
@* this is a comment! *@
Looking at the error
I did as I was told and took a look at the ActivityLog as the error message suggested and opened:
C:\Users\username\AppData\Roaming\Microsoft\VisualStudio\14.0\ActivityLog.xml
Which in turn revealed the following stack trace:
Looking at the stacktrace confirmed my suspicion that on a big picture view something razor specific was messed up (I am looking at you 'RazorSupportedRuntimeVersion'), I also got the info that an exception was being thrown, because a duplicate hashtable entry was being inserted.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Item has already been added. Key in dictionary: 'RazorSupportedRuntimeVersion' Key being added: 'RazorSupportedRuntimeVersion' at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) at System.Collections.Hashtable.Add(Object key, Object value) at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value) at Microsoft.VisualStudio.Utilities.PropertyCollection.AddProperty(Object key, Object property) at Microsoft.VisualStudio.Html.Package.Razor.RazorVersionDetector.Microsoft.Html.Editor.ContainedLanguage.Razor.Def.IRazorVersionDetector.GetVersion(ITextBuffer textBuffer) at Microsoft.Html.Editor.ContainedLanguage.Razor.RazorUtility.TryGetRazorVersion(ITextBuffer textBuffer, Version& razorVersion) at Microsoft.Html.Editor.ContainedLanguage.Razor.RazorErrorTagger..ctor(ITextBuffer textBuffer) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, Object[] args) at Microsoft.Html.Editor.ContainedLanguage.Common.ContainedCodeErrorTaggerProvider`1.CreateTagger[T](ITextBuffer textBuffer) at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.GatherTaggers(ITextBuffer textBuffer)
Google to the rescue
The best part was of course, that an error message gave me something to google for and so I did and finally came to a Visual Studio Feedback Item with the same error message.
The issue is of course marked as closed, but that's neither here nor there.
A first workaround appears
Under workarounds I found out, that resetting your user data seems to fix the issue.
To do that you execute devenv /resetuserdata
and sure enough it did! Intellisense and Highlightning started to work again in the razor view!
But the workaround also has some drawbacks, it well, resets your user data (who would have thought?!), which means it removes all your extensions as well.
Well, whatever. But then it happened to me again a few days later. :|
Improving the workaround
Okay, so I thought, I could probably do better and not reset everything but only the part that causes the problem.
So I took another look at the StackTrace and judging from the StackTrace RazorVersionDetector.GetVersion() seems like the culprit. According to the StackTrace RazorVersionDetector sits in Microsoft.VisualStudio.Html.Package.Razor and luckily I found a dll with the same name under: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\Editors\Microsoft.VisualStudio.Html.Package.dll
.
So I fired up trusty ILSpy and looked at the strange looking interesting interpretation of the C# code of the RazorVersionDetector that ILSpy showed me. There I noticed that it's private methods were called GetCachedVersion() and SetCachedVersion() which was why it worked at first, but broke later on and why resetting the user data worked - it cleared the cache.
Armed with the knowledge I looked for cache folder and luckily I found it: C:\Users\username\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
Removing or renaming this folder forces Visual Studio to recreate it and Razor intellisense starts working again and you get to keep your extensions and stuff.