From bb81fb55004c4a5447170aa7c3039e8e4893728a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 16 Sep 2022 17:46:25 +0300 Subject: [PATCH] Add ApplicationServiceTypes to filter app service types in AddStaticHttpClientProxies and AddHttpClientProxies methods. --- .../ConventionalControllerSetting.cs | 6 --- .../Volo/Abp}/ApplicationServiceTypes.cs | 2 +- ...viceCollectionHttpClientProxyExtensions.cs | 51 +++++++++++++++---- 3 files changed, 42 insertions(+), 17 deletions(-) rename framework/src/{Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services => Volo.Abp.Core/Volo/Abp}/ApplicationServiceTypes.cs (92%) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Conventions/ConventionalControllerSetting.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Conventions/ConventionalControllerSetting.cs index 4700509ea3..787940cc1a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Conventions/ConventionalControllerSetting.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Conventions/ConventionalControllerSetting.cs @@ -7,7 +7,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Reflection; -using Volo.Abp.Application.Services; using Volo.Abp.Reflection; namespace Volo.Abp.AspNetCore.Mvc.Conventions; @@ -121,11 +120,6 @@ public class ConventionalControllerSetting private bool IsPreferredApplicationServiceType(Type type) { - if (ApplicationServiceTypes == ApplicationServiceTypes.All) - { - return true; - } - if (ApplicationServiceTypes == ApplicationServiceTypes.ApplicationServices) { return !type.IsDefined(typeof(IntegrationServiceAttribute)); diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/ApplicationServiceTypes.cs b/framework/src/Volo.Abp.Core/Volo/Abp/ApplicationServiceTypes.cs similarity index 92% rename from framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/ApplicationServiceTypes.cs rename to framework/src/Volo.Abp.Core/Volo/Abp/ApplicationServiceTypes.cs index a9bba71f61..47eeb99d47 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/ApplicationServiceTypes.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/ApplicationServiceTypes.cs @@ -1,6 +1,6 @@ using System; -namespace Volo.Abp.Application.Services; +namespace Volo.Abp; [Flags] public enum ApplicationServiceTypes : byte diff --git a/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionHttpClientProxyExtensions.cs b/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionHttpClientProxyExtensions.cs index 57233ac972..35c5467f78 100644 --- a/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionHttpClientProxyExtensions.cs +++ b/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionHttpClientProxyExtensions.cs @@ -28,14 +28,22 @@ public static class ServiceCollectionHttpClientProxyExtensions /// The name of the remote service configuration to be used by the Static HTTP Client proxies. /// See . /// + /// + /// Can be set to filter the application service types to be registered. + /// Default value: All. + /// public static IServiceCollection AddStaticHttpClientProxies( [NotNull] this IServiceCollection services, [NotNull] Assembly assembly, - [NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName) + [NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName, + ApplicationServiceTypes applicationServiceTypes = ApplicationServiceTypes.All) { Check.NotNull(services, nameof(assembly)); - var serviceTypes = assembly.GetTypes().Where(IsSuitableForClientProxying).ToArray(); + var serviceTypes = assembly + .GetTypes() + .Where(x => IsSuitableForClientProxying(x, applicationServiceTypes)) + .ToArray(); foreach (var serviceType in serviceTypes) { @@ -64,15 +72,23 @@ public static class ServiceCollectionHttpClientProxyExtensions /// /// True, to register the HTTP client proxy as the default implementation for the services. /// + /// + /// Can be set to filter the application service types to be registered. + /// Default value: All. + /// public static IServiceCollection AddHttpClientProxies( [NotNull] this IServiceCollection services, [NotNull] Assembly assembly, [NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName, - bool asDefaultServices = true) + bool asDefaultServices = true, + ApplicationServiceTypes applicationServiceTypes = ApplicationServiceTypes.All) { Check.NotNull(services, nameof(assembly)); - var serviceTypes = assembly.GetTypes().Where(IsSuitableForClientProxying).ToArray(); + var serviceTypes = assembly + .GetTypes() + .Where(x => IsSuitableForClientProxying(x, applicationServiceTypes)) + .ToArray(); foreach (var serviceType in serviceTypes) { @@ -228,13 +244,28 @@ public static class ServiceCollectionHttpClientProxyExtensions /// /// Type to check /// True, if the type is suitable for proxying. Otherwise false. - private static bool IsSuitableForClientProxying(Type type) + private static bool IsSuitableForClientProxying( + Type type, + ApplicationServiceTypes applicationServiceTypes) { - //TODO: Add option to change type filter + if (!type.IsInterface || + !type.IsPublic || + type.IsGenericType || + !typeof(IRemoteService).IsAssignableFrom(type)) + { + return false; + } + + if (applicationServiceTypes == ApplicationServiceTypes.ApplicationServices) + { + return !type.IsDefined(typeof(IntegrationServiceAttribute)); + } + + if (applicationServiceTypes == ApplicationServiceTypes.IntegrationServices) + { + return type.IsDefined(typeof(IntegrationServiceAttribute)); + } - return type.IsInterface - && type.IsPublic - && !type.IsGenericType - && typeof(IRemoteService).IsAssignableFrom(type); + return true; } }