VaryByCustom is not working now after v1.1


In v1.0.1, it was working fine on our website. After upgrading to v1.1, cache doesnt vary by our custom parameters as it used to.

We didnt change any line of code. just upgraded. When we reverted back to 1.0.1, things are working as expected
Closed Sep 11, 2013 at 3:58 PM by moonpyk


DevTrends wrote Jan 10, 2012 at 3:21 PM

Hmm. My tests are all working fine so will have trouble tracking anything down without some more info. Looking at the changeset between version 1.01 and 1.1, I cannot see anything that would affect VaryByCustom.

Does a simple VaryByCustom (see below) work for you?

public override string GetVaryByCustomString(HttpContext context, string custom)
        var isIe = context.Request.UserAgent.ToLowerInvariant().Contains("msie");

        return isIe ? "ie" : "notie";

Can you provide more info. When you say doesn't vary, do you mean that the same cached page is returned regardless?

Is you GetVaryByCustomString method being hit every request?

Are you URLs different for the VaryByCustom requests? Can you give me some examples of strings that are being returned from your GetVaryByCustomString method?

Can you provide your DonutOutputCache definition. Does it use cache profiles or VaryByParam as well.

Slongia wrote Jan 23, 2012 at 12:54 PM

I am getting
The type initializer for 'DevTrends.MvcDonutCaching.OutputCache' threw an exception.
error with version v1.1

My home controller code is
[DonutOutputCache(Duration = 3600, VaryByParam = "none", VaryByCustom = "lang")]
    public ActionResult Index()
        return View();
my global.aspx.cs code is

public override string GetVaryByCustomString(HttpContext context, string value)
        if (value.Equals("lang"))
            return Thread.CurrentThread.CurrentUICulture.Name;
        return base.GetVaryByCustomString(context, value);
I am getting above error even before hitting the GetVaryByCustomString method in global.aspx.cs

DevTrends wrote Jan 23, 2012 at 1:49 PM

@Slongia - not sure if this is the same issue. Does the DonutOutputCache work if you do not vary by custom? The type initializer error suggests a possible problem with the web.config. Do you have a section defined?

As I mentioned to korayem, all my varybycustom examples are working perfectly so it must be some other variable in conjunction with varybycustom that is causing problems. Very hard to diagnose.

If you could attach a sample project, it would help enormously.

You could also/instead download the source and reference that in your project which might result in more helpful information.

Slongia wrote Jan 24, 2012 at 4:01 AM

On looking further deep I found
{"Request for ConfigurationPermission failed while attempting to access configuration section 'system.web/caching/outputCache'. To allow all callers to access the data for this section, set section attribute 'requirePermission' equal 'false' in the configuration file where this section is declared."}

But we have no such section in our web.config file. If we set trust level to full, everything seems fine and code execute without any issue on dev machine.

We have restriction on production machine to set trust level to full. Is there any way we can set "Donut output cache" works with medium trust level?

DevTrends wrote Jan 24, 2012 at 11:39 AM

I have added some code to allow MvcDonutCaching to be used in a strict Medium Trust environment. This is part of the 1.1.1 release which went out this morning to address a serious concurrency issue. Obviously, you will not be able to configure a custom OutputCacheProvider or use cache profiles without permissions for the relevant web.config sections.

Many shared hosting companies make security modifications to the standard Medium Trust settings to allow access to common things such as cache settings so you might want to look into this. Using the requirePermission=false attribute in the web.config/machine.config can help.