Browse Source

Add ApplicationServiceTypes to filter app service types in AddStaticHttpClientProxies and AddHttpClientProxies methods.

pull/14051/head
Halil İbrahim Kalkan 4 years ago
parent
commit
bb81fb5500
  1. 6
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Conventions/ConventionalControllerSetting.cs
  2. 2
      framework/src/Volo.Abp.Core/Volo/Abp/ApplicationServiceTypes.cs
  3. 51
      framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionHttpClientProxyExtensions.cs

6
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));

2
framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/ApplicationServiceTypes.cs → 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

51
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 <see cref="AbpRemoteServiceOptions"/>.
/// </param>
/// <param name="applicationServiceTypes">
/// Can be set to filter the application service types to be registered.
/// Default value: All.
/// </param>
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
/// <param name="asDefaultServices">
/// True, to register the HTTP client proxy as the default implementation for the services.
/// </param>
/// <param name="applicationServiceTypes">
/// Can be set to filter the application service types to be registered.
/// Default value: All.
/// </param>
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
/// </summary>
/// <param name="type">Type to check</param>
/// <returns>True, if the type is suitable for proxying. Otherwise false.</returns>
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;
}
}

Loading…
Cancel
Save