Skip to content

Commit

Permalink
Merge pull request #30 from Kentico/MVCHF11-3-initialization-exception
Browse files Browse the repository at this point in the history
MVCHF11-3 - Fix issue with concurrent GlobalFilters registration
  • Loading branch information
KenticoPavelV authored Mar 16, 2018
2 parents 34377a5 + 913ea1c commit 9e7c18f
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@

## Kentico.Content.Web.Mvc

### 2.0.1 (2018-03-16)
#### Fixed
* Fix concurrent GlobalFilters registration which led to failed application initialization under high traffic.
[#30](https://github.com/Kentico/Mvc/pull/30)

### 2.0.0 (2017-12-11)
#### Release notes
* This package version supports integration with Kentico 11.
Expand Down
2 changes: 2 additions & 0 deletions src/Kentico.Content.Web.Mvc/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static class ApplicationBuilderExtensions
/// <param name="builder">The application builder.</param>
public static void UsePreview(this ApplicationBuilder builder)
{
PreviewGlobalFilters.Register();

var module = new PreviewFeatureModule();
builder.RegisterModule(module);
}
Expand Down
1 change: 1 addition & 0 deletions src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="HelperMethods\UrlHelperFileMethods.cs" />
<Compile Include="IFeatureSetExtensions.cs" />
<Compile Include="Preview\IPreviewFeature.cs" />
<Compile Include="Preview\PreviewGlobalFilters.cs" />
<Compile Include="Preview\PreviewOutputCacheFilter.cs" />
<Compile Include="Preview\PreviewFeature.cs" />
<Compile Include="Preview\PreviewFeatureModule.cs" />
Expand Down
3 changes: 1 addition & 2 deletions src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;

using CMS.DocumentEngine;
using CMS.Helpers;

using Kentico.Web.Mvc;

namespace Kentico.Content.Web.Mvc
Expand All @@ -22,7 +22,6 @@ internal sealed class PreviewFeatureModule : IModule
public void Initialize(HttpApplication application)
{
application.BeginRequest += HandleBeginRequest;
GlobalFilters.Filters.Add(new PreviewOutputCacheFilter());
}


Expand Down
25 changes: 25 additions & 0 deletions src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Linq;
using System.Web.Mvc;

namespace Kentico.Content.Web.Mvc
{
/// <summary>
/// Class that provides registration of all the required global filters for the page preview feature.
/// </summary>
internal static class PreviewGlobalFilters
{
/// <summary>
/// Registers global filters for the Preview feature.
/// </summary>
public static void Register()
{
var globalFilters = GlobalFilters.Filters;

// Add a filter to disable output cache for preview
if (!globalFilters.Any(f => f.Instance.GetType().Equals(typeof(PreviewOutputCacheFilter))))
{
globalFilters.Add(new PreviewOutputCacheFilter());
}
}
}
}
2 changes: 1 addition & 1 deletion src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
[assembly: Guid("430a1add-4583-459f-879c-a22a61e44a3b")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]
[assembly: AssemblyInformationalVersion("2.0.1")]

[assembly: InternalsVisibleTo("Kentico.Content.Web.Mvc.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="HelperMethods\NameValueCollectionExtensionsTests.cs" />
<Compile Include="HelperMethods\SizeConstraintTests.cs" />
<Compile Include="HelperMethods\UrlHelperFileMethodsTests.cs" />
<Compile Include="Preview\PreviewGlobalFiltersTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Linq;
using System.Web.Mvc;

using CMS.Tests;

using NUnit.Framework;

namespace Kentico.Content.Web.Mvc.Tests
{
/// <summary>
/// Unit tests for class <see cref="PreviewGlobalFiltersTests"/>.
/// </summary>
public class PreviewGlobalFiltersTests
{
[TestFixture]
[Category.Unit]
public class RegisterTests
{
[SetUp]
public void SetUp()
{
GlobalFilters.Filters.Clear();
}


[OneTimeTearDown]
public void OneTimeTearDown()
{
GlobalFilters.Filters.Clear();
}


[Test]
public void Register_SingleCall_PreviewFilterRegistered()
{
PreviewGlobalFilters.Register();

CMSAssert.All(
() => Assert.That(GlobalFilters.Filters.Count, Is.EqualTo(1)),
() => Assert.That(GlobalFilters.Filters.First().Instance, Is.TypeOf(typeof(PreviewOutputCacheFilter)))
);
}


[Test]
public void Register_TwoCalls_PreviewFilterRegisteredOnlyOnce()
{
PreviewGlobalFilters.Register();
PreviewGlobalFilters.Register();

CMSAssert.All(
() => Assert.That(GlobalFilters.Filters.Count, Is.EqualTo(1)),
() => Assert.That(GlobalFilters.Filters.First().Instance, Is.TypeOf(typeof(PreviewOutputCacheFilter)))
);
}

}
}
}

0 comments on commit 9e7c18f

Please sign in to comment.