From 6944d7836d165485a62bd693dd73f385943c5b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 1 Jun 2020 22:42:38 +0300 Subject: [PATCH] Use ApplicationParts.AddIfNotContains for the AbpAspNetCoreMvcModule --- .../AbpMvcBuilderExtensions.cs | 17 ++++++++++++++--- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 9 ++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/Extensions/DependencyInjection/AbpMvcBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/Extensions/DependencyInjection/AbpMvcBuilderExtensions.cs index 77b517bc45..c7f3777466 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/Extensions/DependencyInjection/AbpMvcBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/Extensions/DependencyInjection/AbpMvcBuilderExtensions.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationParts; @@ -8,13 +9,23 @@ namespace Microsoft.Extensions.DependencyInjection { public static void AddApplicationPartIfNotExists(this IMvcBuilder mvcBuilder, Assembly assembly) { - if (mvcBuilder.PartManager.ApplicationParts.Any( + mvcBuilder.PartManager.ApplicationParts.AddIfNotContains(assembly); + } + + public static void AddApplicationPartIfNotExists(this IMvcCoreBuilder mvcCoreBuilder, Assembly assembly) + { + mvcCoreBuilder.PartManager.ApplicationParts.AddIfNotContains(assembly); + } + + public static void AddIfNotContains(this IList applicationParts, Assembly assembly) + { + if (applicationParts.Any( p => p is AssemblyPart assemblyPart && assemblyPart.Assembly == assembly)) { return; } - mvcBuilder.PartManager.ApplicationParts.Add(new AssemblyPart(assembly)); + applicationParts.Add(new AssemblyPart(assembly)); } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index 1657ae10c1..a923fee3f1 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -161,7 +161,7 @@ namespace Volo.Abp.AspNetCore.Mvc var application = context.Services.GetSingletonInstance(); partManager.FeatureProviders.Add(new AbpConventionalControllerFeatureProvider(application)); - partManager.ApplicationParts.Add(new AssemblyPart(typeof(AbpAspNetCoreMvcModule).Assembly)); + partManager.ApplicationParts.AddIfNotContains(typeof(AbpAspNetCoreMvcModule).Assembly); Configure(mvcOptions => { @@ -220,12 +220,7 @@ namespace Volo.Abp.AspNetCore.Mvc { foreach (var moduleAssembly in moduleAssemblies) { - if (partManager.ApplicationParts.OfType().Any(p => p.Assembly == moduleAssembly)) - { - continue; - } - - partManager.ApplicationParts.Add(new AssemblyPart(moduleAssembly)); + partManager.ApplicationParts.AddIfNotContains(moduleAssembly); } } }