Browse Source

Add AbpAutofacConstructorFinder

pull/6569/head
maliming 6 years ago
parent
commit
cc7480f6df
  1. 3
      framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs
  2. 43
      framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AbpAutofacConstructorFinder.cs

3
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);
}
}

43
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<Type, ConstructorInfo[]> _finder;
private static readonly ConcurrentDictionary<Type, ConstructorInfo[]> DefaultPublicConstructorsCache = new ConcurrentDictionary<Type, ConstructorInfo[]>();
public AbpAutofacConstructorFinder()
: this(GetDefaultPublicConstructors)
{
}
public AbpAutofacConstructorFinder(Func<Type, ConstructorInfo[]> 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;
}
}
}
Loading…
Cancel
Save