From cc7480f6dfa4a144b1f53ddfca874c1ac11ae95b Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 10 Dec 2020 16:07:06 +0800 Subject: [PATCH] Add AbpAutofacConstructorFinder --- .../AutofacRegistration.cs | 3 ++ .../Autofac/AbpAutofacConstructorFinder.cs | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AbpAutofacConstructorFinder.cs diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs index 024fc3a258..e0f7ec50ad 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs @@ -27,6 +27,7 @@ using System; using System.Reflection; using Autofac.Builder; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; using Volo.Abp.Modularity; namespace Autofac.Extensions.DependencyInjection @@ -116,6 +117,7 @@ namespace Autofac.Extensions.DependencyInjection .RegisterGeneric(service.ImplementationType) .As(service.ServiceType) .ConfigureLifecycle(service.Lifetime) + .FindConstructorsWith(new AbpAutofacConstructorFinder()) .ConfigureAbpConventions(moduleContainer, registrationActionList); } else @@ -124,6 +126,7 @@ namespace Autofac.Extensions.DependencyInjection .RegisterType(service.ImplementationType) .As(service.ServiceType) .ConfigureLifecycle(service.Lifetime) + .FindConstructorsWith(new AbpAutofacConstructorFinder()) .ConfigureAbpConventions(moduleContainer, registrationActionList); } } diff --git a/framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AbpAutofacConstructorFinder.cs b/framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AbpAutofacConstructorFinder.cs new file mode 100644 index 0000000000..a9d0b736a2 --- /dev/null +++ b/framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AbpAutofacConstructorFinder.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Concurrent; +using System.Reflection; +using Autofac.Core.Activators.Reflection; + +namespace Volo.Abp.Autofac +{ + public class AbpAutofacConstructorFinder : IConstructorFinder + { + private const BindingFlags DeclaredOnlyPublicFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; //Remove static constructor, BindingFlags.Static + + private readonly Func _finder; + + private static readonly ConcurrentDictionary DefaultPublicConstructorsCache = new ConcurrentDictionary(); + + public AbpAutofacConstructorFinder() + : this(GetDefaultPublicConstructors) + { + } + + public AbpAutofacConstructorFinder(Func finder) + { + _finder = finder ?? throw new ArgumentNullException(nameof(finder)); + } + + public ConstructorInfo[] FindConstructors(Type targetType) + { + return _finder(targetType); + } + + private static ConstructorInfo[] GetDefaultPublicConstructors(Type type) + { + var retval = DefaultPublicConstructorsCache.GetOrAdd(type, t => t.GetConstructors(DeclaredOnlyPublicFlags)); + + if (retval.Length == 0) + { + throw new NoConstructorsFoundException(type); + } + + return retval; + } + } +}