From 6a62741db62018388347febf1c0edf6b56573017 Mon Sep 17 00:00:00 2001 From: rpribyl Date: Thu, 15 Mar 2018 13:49:00 +0100 Subject: [PATCH 1/4] MVCHF11-3 - Fix issue with concurrent GlobalFilters registration --- .../ApplicationBuilderExtensions.cs | 2 ++ .../Kentico.Content.Web.Mvc.csproj | 1 + .../Preview/PreviewFeatureModule.cs | 3 +-- .../Preview/PreviewGlobalFilters.cs | 25 +++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs 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..afa42db --- /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.GetType().Equals(typeof(PreviewOutputCacheFilter)))) + { + globalFilters.Add(new PreviewOutputCacheFilter()); + } + } + } +} From 30613365c155d2e1b569141fcbf6e6e1ccc8f940 Mon Sep 17 00:00:00 2001 From: rpribyl Date: Thu, 15 Mar 2018 15:00:21 +0100 Subject: [PATCH 2/4] MVCHF11-3 - Update Assembly version & changelog --- CHANGELOG.md | 5 +++++ src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd41081..45cb999 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. +[#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/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 From 935c58cb7f394f4ae047d08ec5c1f7b609e02f42 Mon Sep 17 00:00:00 2001 From: rpribyl Date: Thu, 15 Mar 2018 15:32:50 +0100 Subject: [PATCH 3/4] MVCHF11-3 - Fix multiple filter registration & add tests --- .../Preview/PreviewGlobalFilters.cs | 2 +- .../Kentico.Content.Web.Mvc.Tests.csproj | 1 + .../Preview/PreviewGlobalFiltersTests.cs | 59 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/Kentico.Content.Web.Mvc.Tests/Preview/PreviewGlobalFiltersTests.cs diff --git a/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs b/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs index afa42db..f41c215 100644 --- a/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs +++ b/src/Kentico.Content.Web.Mvc/Preview/PreviewGlobalFilters.cs @@ -16,7 +16,7 @@ public static void Register() var globalFilters = GlobalFilters.Filters; // Add a filter to disable output cache for preview - if (!globalFilters.Any(f => f.GetType().Equals(typeof(PreviewOutputCacheFilter)))) + if (!globalFilters.Any(f => f.Instance.GetType().Equals(typeof(PreviewOutputCacheFilter)))) { globalFilters.Add(new PreviewOutputCacheFilter()); } 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))) + ); + } + + } + } +} From 913ea1cdeaf3897ecf74d576ce51474631bee6c5 Mon Sep 17 00:00:00 2001 From: Radek Pribyl Date: Thu, 15 Mar 2018 16:13:11 +0100 Subject: [PATCH 4/4] MVCHF11-3 - Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45cb999..17c7af1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,7 +83,7 @@ ### 2.0.1 (2018-03-16) #### Fixed -* Fix concurrent GlobalFilters registration. +* 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)