Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Commented Issue: Multiple DisplayModes - Caching error, will show wrong View [280]

$
0
0
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.


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

Comments: I'm seeing this issue as well. I see that it is fixed vNext but is there and ETA?

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>