1

Closed

NullReferenceException in DevTrends.MvcDonutCaching.KeyGenerator.GenerateKey...

description

I'm having the problem described by @Aviv in this post:
http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3#125

I looked for the error here but not found on codeplex

The environment is Medium Trust!

---- Route: ---

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.IgnoreRoute("elmah.axd");

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new[] { "LetsGo.Controllers" }
);
}

---- Controller: ---

[DonutOutputCache(CacheProfile = "StaticPage")]
public ActionResult Index()
{
    return View(_themeParty.Repository.Query().Published(true).ToList());
}

[ChildActionOnly]
[DonutOutputCache(CacheProfile = "DataCache")]
public PartialViewResult Subsidiaries() { return PartialView(_subsidiaries.Repository.Query().Published().ToList()); }


---- Web.config ----
<caching>
<outputCacheSettings>
<outputCacheProfiles>
    <add name="DataCache" enabled="true" duration="10800" varyByParam="*" />
    <add name="StaticPage" enabled="true" duration="21600" varyByParam="*" />
    <add name="ByHeader" enabled="true" duration="300" varyByHeader="*" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>


---- _Layout.cshtml -----
...
@Html.Action("Subsidiaries", "Home", new { area = "" })
...


Full error:
Server Error in '/LetsGo' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
DevTrends.MvcDonutCaching.KeyGenerator.GenerateKey(ControllerContext context, CacheSettings cacheSettings) +777
DevTrends.MvcDonutCaching.DonutOutputCacheAttribute.ExecuteCallback(ControllerContext context, Boolean hasErrors) +43
System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception) +168
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +652
System.Web.Mvc.Controller.ExecuteCore() +161
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +304
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +61
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +461
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +370

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
Closed Sep 11, 2013 at 4:04 PM by moonpyk

comments

DevTrends wrote Feb 16, 2012 at 11:39 AM

Thank you for the comprehensive description. I have tracked down the problem. Because you have a strict medium trust environment, MvcDonutCaching cannot access the ConfigurationSection from the web.config that stores cache profiles. I have improved the code to provide a meaningful error message, but without that permission, it is not possible to use cache profiles. Most hosting environments modify the Medium trust settings to allow access to configuration sections, so if you own the server, this is one option open to you. Otherwise, to use MvcDonutCaching, you will need to avoid cache profiles and add duration to the attribute itself.

If you or anyone else have any suggestions for a workaround, I am happy to hear them.

Ridermansb wrote Feb 16, 2012 at 1:05 PM

I'm not sure how this level of confidence, but to send mail access Web.config to get the recipient:

ConfigurationManager.AppSettings["toEmail"]

And it works perfectly ... if this is the case then maybe some parts of the web.config is that they are restricted?

DevTrends wrote Feb 16, 2012 at 1:31 PM

Standard things like appsettings and connectionstrings can be read, but other configuration sections are restricted by default in medium trust. This might help:

http://www.devproconnections.com/article/security-development/is-requirepermission-a-hack-or-a-useful-feature