Skip to content

Commit 38752fa

Browse files
author
DevTrends
committed
Fixed serious concurrency issue.
Allowed use in Medium Trust.
1 parent 42d4934 commit 38752fa

File tree

8 files changed

+55
-25
lines changed

8 files changed

+55
-25
lines changed

DevTrends.MvcDonutCaching/CacheSettingsManager.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Configuration;
2+
using System.Diagnostics;
3+
using System.Security;
24
using System.Web;
35
using System.Web.Configuration;
46

@@ -11,7 +13,16 @@ public class CacheSettingsManager : ICacheSettingsManager
1113

1214
public CacheSettingsManager()
1315
{
14-
_outputCacheSection = (OutputCacheSection)ConfigurationManager.GetSection("system.web/caching/outputCache");
16+
try
17+
{
18+
_outputCacheSection = (OutputCacheSection)ConfigurationManager.GetSection("system.web/caching/outputCache");
19+
}
20+
catch (SecurityException)
21+
{
22+
Debug.WriteLine("MvcDonutCaching does not have permission to read web.config section. Using default provider.");
23+
_outputCacheSection = new OutputCacheSection { DefaultProviderName = AspnetInternalProviderName, EnableOutputCache = true };
24+
}
25+
1526
}
1627

1728
public string RetrieveOutputCacheProviderType()

DevTrends.MvcDonutCaching/DonutOutputCacheAttribute.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
using System;
22
using System.Globalization;
33
using System.IO;
4+
using System.Web;
45
using System.Web.Mvc;
56
using System.Web.UI;
6-
using System.Web;
77

88
namespace DevTrends.MvcDonutCaching
99
{
1010
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
1111
public class DonutOutputCacheAttribute : ActionFilterAttribute, IExceptionFilter
1212
{
13-
private const string CallbackKey = "d0nutCallback";
14-
1513
private readonly IKeyGenerator _keyGenerator;
1614
private readonly IDonutHoleFiller _donutHoleFiller;
1715
private readonly IExtendedOutputCacheManager _outputCacheManager;
1816
private readonly ICacheSettingsManager _cacheSettingsManager;
1917
private readonly ICacheHeadersHelper _cacheHeadersHelper;
2018

2119
private CacheSettings _cacheSettings;
22-
private string _cacheKey;
2320

2421
public int Duration { get; set; }
2522
public string VaryByParam { get; set; }
@@ -45,11 +42,11 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
4542
{
4643
_cacheSettings = BuildCacheSettings();
4744

45+
var cacheKey = _keyGenerator.GenerateKey(filterContext, _cacheSettings);
46+
4847
if (_cacheSettings.IsServerCachingEnabled)
4948
{
50-
_cacheKey = _keyGenerator.GenerateKey(filterContext, _cacheSettings);
51-
52-
var cachedItem = _outputCacheManager.GetItem(_cacheKey);
49+
var cachedItem = _outputCacheManager.GetItem(cacheKey);
5350

5451
if (cachedItem != null)
5552
{
@@ -63,17 +60,15 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
6360

6461
if (filterContext.Result == null)
6562
{
66-
var callbackKey = BuildCallbackKey(filterContext);
67-
6863
var cachingWriter = new StringWriter(CultureInfo.InvariantCulture);
6964

7065
var originalWriter = filterContext.HttpContext.Response.Output;
7166

7267
filterContext.HttpContext.Response.Output = cachingWriter;
7368

74-
filterContext.HttpContext.Items[callbackKey] = new Action<bool>(hasErrors =>
69+
filterContext.HttpContext.Items[cacheKey] = new Action<bool>(hasErrors =>
7570
{
76-
filterContext.HttpContext.Items.Remove(callbackKey);
71+
filterContext.HttpContext.Items.Remove(cacheKey);
7772

7873
filterContext.HttpContext.Response.Output = originalWriter;
7974

@@ -89,7 +84,7 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
8984

9085
if (_cacheSettings.IsServerCachingEnabled && filterContext.HttpContext.Response.StatusCode == 200)
9186
{
92-
_outputCacheManager.AddItem(_cacheKey, cacheItem, DateTime.Now.AddSeconds(_cacheSettings.Duration));
87+
_outputCacheManager.AddItem(cacheKey, cacheItem, DateTime.Now.AddSeconds(_cacheSettings.Duration));
9388
}
9489
}
9590
});
@@ -113,24 +108,16 @@ public void OnException(ExceptionContext filterContext)
113108

114109
private void ExecuteCallback(ControllerContext context, bool hasErrors)
115110
{
116-
var callbackKey = BuildCallbackKey(context);
111+
var cacheKey = _keyGenerator.GenerateKey(context, _cacheSettings);
117112

118-
var callback = context.HttpContext.Items[callbackKey] as Action<bool>;
113+
var callback = context.HttpContext.Items[cacheKey] as Action<bool>;
119114

120115
if (callback != null)
121116
{
122117
callback.Invoke(hasErrors);
123118
}
124119
}
125120

126-
private string BuildCallbackKey(ControllerContext context)
127-
{
128-
var actionName = context.RouteData.Values["action"].ToString();
129-
var controllerName = context.RouteData.Values["controller"].ToString();
130-
131-
return string.Format("{0}.{1}.{2}", CallbackKey, controllerName, actionName);
132-
}
133-
134121
private CacheSettings BuildCacheSettings()
135122
{
136123
CacheSettings cacheSettings;

DevTrends.MvcDonutCaching/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
[assembly: AssemblyCulture("")]
1212
[assembly: ComVisible(false)]
1313
[assembly: Guid("ebcc3291-f04a-4511-b7eb-ddf57a74ada9")]
14-
[assembly: AssemblyVersion("1.1.0")]
15-
[assembly: AssemblyFileVersion("1.1.0")]
14+
[assembly: AssemblyVersion("1.1.1")]
15+
[assembly: AssemblyFileVersion("1.1.1")]

nuget/Build.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nuget pack
2+
3+
pause

nuget/MvcDonutCaching.nuspec

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>MvcDonutCaching</id>
5+
<version>1.1.1</version>
6+
<authors>Paul Hiles</authors>
7+
<owners>DevTrends</owners>
8+
<licenseUrl>http://mvcdonutcaching.codeplex.com/license</licenseUrl>
9+
<projectUrl>http://mvcdonutcaching.codeplex.com/</projectUrl>
10+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
11+
<description>MvcDonutCaching provides extensible donut output caching for ASP.NET MVC 3 and above.</description>
12+
<tags>mvc3 donut caching mvc outputcaching substitution</tags>
13+
</metadata>
14+
</package>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Getting started with DevTrends.MvcDonutCaching
2+
----------------------------------------------
3+
4+
Find out how to use DevTrends.MvcDonutCaching by visiting http://mvcdonutcaching.codeplex.com/
5+
6+
Please report all bugs and feature requests on CodePlex.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<configuration>
2+
<system.web.webPages.razor>
3+
<pages>
4+
<namespaces>
5+
<add namespace="DevTrends.MvcDonutCaching" />
6+
</namespaces>
7+
</pages>
8+
</system.web.webPages.razor>
9+
</configuration>
24 KB
Binary file not shown.

0 commit comments

Comments
 (0)