diff --git a/CHANGELOG.md b/CHANGELOG.md index cd41081..17c7af1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/Kentico.Content.Web.Mvc/ApplicationBuilderExtensions.cs b/src/Kentico.Content.Web.Mvc/ApplicationBuilderExtensions.cs index 3808834..c0e66b2 100644 --- a/src/Kentico.Content.Web.Mvc/ApplicationBuilderExtensions.cs +++ b/src/Kentico.Content.Web.Mvc/ApplicationBuilderExtensions.cs @@ -13,6 +13,8 @@ public static class ApplicationBuilderExtensions /// The application builder. public static void UsePreview(this ApplicationBuilder builder) { + PreviewGlobalFilters.Register(); + var module = new PreviewFeatureModule(); builder.RegisterModule(module); } diff --git a/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj b/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj index 03b9549..8b63ef1 100644 --- a/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj +++ b/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj @@ -51,6 +51,7 @@ + diff --git a/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs b/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs index 9b882a0..ae6dfc8 100644 --- a/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs +++ b/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs @@ -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 @@ -22,7 +22,6 @@ internal sealed class PreviewFeatureModule : IModule public void Initialize(HttpApplication application) { application.BeginRequest += HandleBeginRequest; - GlobalFilters.Filters.Add(new PreviewOutputCacheFilter()); } diff --git a/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs b/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs new file mode 100644 index 0000000..f41c215 --- /dev/null +++ b/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs @@ -0,0 +1,25 @@ +using System.Linq; +using System.Web.Mvc; + +namespace Kentico.Content.Web.Mvc +{ + /// + /// Class that provides registration of all the required global filters for the page preview feature. + /// + internal static class PreviewGlobalFilters + { + /// + /// Registers global filters for the Preview feature. + /// + 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()); + } + } + } +} diff --git a/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs b/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs index bef4d79..a90e04c 100644 --- a/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs +++ b/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file diff --git a/test/Kentico.Content.Web.Mvc.Tests/Kentico.Content.Web.Mvc.Tests.csproj b/test/Kentico.Content.Web.Mvc.Tests/Kentico.Content.Web.Mvc.Tests.csproj index 018ffbb..2773a9e 100644 --- a/test/Kentico.Content.Web.Mvc.Tests/Kentico.Content.Web.Mvc.Tests.csproj +++ b/test/Kentico.Content.Web.Mvc.Tests/Kentico.Content.Web.Mvc.Tests.csproj @@ -47,6 +47,7 @@ + diff --git a/test/Kentico.Content.Web.Mvc.Tests/Preview/PreviewGlobalFiltersTests.cs b/test/Kentico.Content.Web.Mvc.Tests/Preview/PreviewGlobalFiltersTests.cs new file mode 100644 index 0000000..f4ad38b --- /dev/null +++ b/test/Kentico.Content.Web.Mvc.Tests/Preview/PreviewGlobalFiltersTests.cs @@ -0,0 +1,59 @@ +using System.Linq; +using System.Web.Mvc; + +using CMS.Tests; + +using NUnit.Framework; + +namespace Kentico.Content.Web.Mvc.Tests +{ + /// + /// Unit tests for class . + /// + 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))) + ); + } + + } + } +}