I have large issue with one of my sites where i have implemted multiple views side by side using MVC4 DisplayModes.
Pages are like
MembershipPage.cshtml
MembershipPage.Mobile.cshtml
_Layout.cshtml
_Layout.Mobile.cshtml
The pages render just fine after having just started the page, however after some time mobile devices will start showing the desktop view but in the Mobile layout page.
What seems to be happening is that the RazorViewEngine has some kind of timed caching for the views, and since my desktop views are used way more than the mobile counterparts the mobile views timeout and the desktop ones are then served instead.
I have currently solved this by overriding the RazorViewEngine's FindPartialView and FindView and setting useCache to false. However this is naturally a huge performance issue.
I have checked the source code and to my opinion this is what is happening:
Within the GetPathFromGeneralName all DisplayMode views are cached. However since they are not accessed every time a specific view is rendered some views will timeout before all timeout, resulting in wrong views being returned.
If my opinion here is wrong, please do tell me what else can be happening because i get some very wierd looking pages with this error in place.
Unfortunately I can't help you as I'm experiencing a similar thing. For me it was also happening in Beta. I have been having trouble trying to reproduce the error, are you able to describe the simplest scenario to reproduce this error? Is it something like:
1. mobile views requested and cached
2. desktop views requested and cached.
3. mobile views cache timed out after 15 min
4. mobile views requested and desktop cached views gets served?
I have created a solution with an MVC site, this site has one View Index.cshtml and a Mobile counter part Index.Mobile.cshtml.
It is very important not to run in debug compilation <compilation debug="false" targetFramework="4.0" /> I have already set this in the web.config, the reason for this is that this code resides inside the VirtualPathProviderViewEngine constructor.
if (HttpContext.Current == null || HttpContext.Current.IsDebuggingEnabled) {
ViewLocationCache = DefaultViewLocationCache.Null;
} else {
ViewLocationCache = new DefaultViewLocationCache();
}
The solution also has a very simple winforms application that you simply need to run it will then show you the issue on a live site i have put up on
http://mvcbug.flexybox.com. (AS I HAVE NOW PUBLISHED A LINK BOTH HERE AND IN THE APPLICATION, AND AS THIS ERROR DEPENDS ON WHO ACCESSES THIS LINK WITHIN 16 MINUTES YOU SHOULD HOST YOUR OWN SITE TO TEST THIS)
You will need to let it run for 16 minutes ( i have added a minute to ensure that the cache runs out ).
Initially i request both views, this is actually not neccesary but it shows you that the site does have a Mobile view. MVC will automatically cache all the views for the different displaymodes for the view that is requested. Meaning if you request just the
Desktop view, the mobile view will also be added to the cache, this is why the mobile view works correctly as long as it is requested continually.
This application requests the desktop site every 15 seconds, this way the sliding expiration on the cache is used on the desktop view, which means the desktop view stays in the cache. After the full 16 minutes, the application will then request the mobile
view which will actually return the desktop view because mvc will check the cache before checking for the actual views in the view folders.
In the MVC project there is also a ShortMemoryEngine (you just need to comment it in global.asax), i used this when creating the app because the only thing it does is replace the 15 minute cache with a 1 minute cache.
Thank you mSchmidt for the explanation and well done. I am now able to exactly reproduce the error thanks to you - it seemed intermittent to me up until now, and never occurred in dev!
I did not use your example but instead my own consisting only of an index.cshtml and index.mobile.cshtml, and suredly enough after 16 mins the browser with user agent set to the mobile device no longer was showing index.mobile.cshtml!
Hi Yes it also took abit of source code exploration to figure out what was happening. I have tried reporting the bug but the page says it cannot be displayed. https://connect.microsoft.com/VisualStudio/feedback/CreateFeedbackForm.aspx?FeedbackFormConfigurationID=4861&FeedbackType=1
So i guess this is my bug report :)
mSchmidt
0 Points
3 Posts
MVC 4 RC - Mobile View Cache bug.
Jul 14, 2012 07:17 PM|LINK
Hi
I have large issue with one of my sites where i have implemted multiple views side by side using MVC4 DisplayModes.
Pages are like
MembershipPage.cshtml
MembershipPage.Mobile.cshtml
_Layout.cshtml
_Layout.Mobile.cshtml
The pages render just fine after having just started the page, however after some time mobile devices will start showing the desktop view but in the Mobile layout page.
What seems to be happening is that the RazorViewEngine has some kind of timed caching for the views, and since my desktop views are used way more than the mobile counterparts the mobile views timeout and the desktop ones are then served instead.
I have currently solved this by overriding the RazorViewEngine's FindPartialView and FindView and setting useCache to false. However this is naturally a huge performance issue.
I have checked the source code and to my opinion this is what is happening:
This is the cache being used, it has a default timeout of 15 minutes.
http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/3b39178d07d6#src%2fSystem.Web.Mvc%2fDefaultViewLocationCache.cs
The VirtualPathProviderViewEngine, uses this to cache the views, however the implementation of this does not take into account the fact that some views will timeout before other DisplayMode views.
http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/3b39178d07d6#src%2fSystem.Web.Mvc%2fVirtualPathProviderViewEngine.cs
Within the GetPathFromGeneralName all DisplayMode views are cached. However since they are not accessed every time a specific view is rendered some views will timeout before all timeout, resulting in wrong views being returned.
If my opinion here is wrong, please do tell me what else can be happening because i get some very wierd looking pages with this error in place.
thuanto
Member
8 Points
4 Posts
Re: MVC 4 RC - Mobile View Cache bug.
Jul 15, 2012 03:05 AM|LINK
Hi,
Unfortunately I can't help you as I'm experiencing a similar thing. For me it was also happening in Beta. I have been having trouble trying to reproduce the error, are you able to describe the simplest scenario to reproduce this error? Is it something like:
1. mobile views requested and cached
2. desktop views requested and cached.
3. mobile views cache timed out after 15 min
4. mobile views requested and desktop cached views gets served?
Thanks.
ignatandrei
All-Star
134531 Points
21581 Posts
Moderator
MVP
Re: MVC 4 RC - Mobile View Cache bug.
Jul 15, 2012 03:07 AM|LINK
Could you reproduce this on a single simple project, no database involved, and say what's the request URL order and what( desktop, mobile)?
mSchmidt
0 Points
3 Posts
Re: MVC 4 RC - Mobile View Cache bug.
Jul 15, 2012 09:50 AM|LINK
I can do you one better.
I have created a solution with an MVC site, this site has one View Index.cshtml and a Mobile counter part Index.Mobile.cshtml.
It is very important not to run in debug compilation <compilation debug="false" targetFramework="4.0" /> I have already set this in the web.config, the reason for this is that this code resides inside the VirtualPathProviderViewEngine constructor.
if (HttpContext.Current == null || HttpContext.Current.IsDebuggingEnabled) { ViewLocationCache = DefaultViewLocationCache.Null; } else { ViewLocationCache = new DefaultViewLocationCache(); }The solution also has a very simple winforms application that you simply need to run it will then show you the issue on a live site i have put up on http://mvcbug.flexybox.com. (AS I HAVE NOW PUBLISHED A LINK BOTH HERE AND IN THE APPLICATION, AND AS THIS ERROR DEPENDS ON WHO ACCESSES THIS LINK WITHIN 16 MINUTES YOU SHOULD HOST YOUR OWN SITE TO TEST THIS)
You will need to let it run for 16 minutes ( i have added a minute to ensure that the cache runs out ).
Initially i request both views, this is actually not neccesary but it shows you that the site does have a Mobile view. MVC will automatically cache all the views for the different displaymodes for the view that is requested. Meaning if you request just the Desktop view, the mobile view will also be added to the cache, this is why the mobile view works correctly as long as it is requested continually.
This application requests the desktop site every 15 seconds, this way the sliding expiration on the cache is used on the desktop view, which means the desktop view stays in the cache. After the full 16 minutes, the application will then request the mobile view which will actually return the desktop view because mvc will check the cache before checking for the actual views in the view folders.
In the MVC project there is also a ShortMemoryEngine (you just need to comment it in global.asax), i used this when creating the app because the only thing it does is replace the 15 minute cache with a 1 minute cache.
I have put the solution up here: http://mvcbug.flexybox.com/Content/MVC_CACHE_BUG.zip
thuanto
Member
8 Points
4 Posts
Re: MVC 4 RC - Mobile View Cache bug.
Jul 15, 2012 12:08 PM|LINK
Thank you mSchmidt for the explanation and well done. I am now able to exactly reproduce the error thanks to you - it seemed intermittent to me up until now, and never occurred in dev!
I did not use your example but instead my own consisting only of an index.cshtml and index.mobile.cshtml, and suredly enough after 16 mins the browser with user agent set to the mobile device no longer was showing index.mobile.cshtml!
Have you logged a bug for this?
mSchmidt
0 Points
3 Posts
Re: MVC 4 RC - Mobile View Cache bug.
Jul 15, 2012 12:35 PM|LINK
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: MVC 4 RC - Mobile View Cache bug.
Jul 16, 2012 01:06 AM|LINK
Thanks for reporting this bug. We have confirmed it is a bug. I've notified the team.
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: MVC 4 RC - Mobile View Cache bug.
Jul 16, 2012 08:34 AM|LINK
Can you file a bug here? http://aspnetwebstack.codeplex.com/WorkItem/Create?ProjectName=aspnetwebstack Make sure ASP.NET MVC is selected for the "Component" field.
ASP.NET PM
http://about.me/erikporter
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: MVC 4 RC - Mobile View Cache bug.
Jul 16, 2012 11:10 PM|LINK
Thanks again for reporting this bug. It looks like you opened the following bug report: http://aspnetwebstack.codeplex.com/workitem/280
jpenneck
Member
39 Points
9 Posts
Re: MVC 4 RC - Mobile View Cache bug.
Jul 19, 2012 12:11 AM|LINK
Does anyone know of a simple work-around for this ? Hopefully this will be fixed in the final release. Thanks.