Skip to content

Commit

Permalink
Make sure we don't re-decorate the same service types multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Aug 25, 2020
1 parent 3f8e1e1 commit fe01bc6
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Scrutor/ServiceCollectionExtensions.Decoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,29 +239,32 @@ private static IServiceCollection DecorateOpenGeneric(this IServiceCollection se
throw error;
}

private static bool IsSameGenericType(Type t1, Type t2)
private static bool HasSameTypeDefinition(Type t1, Type t2)
{
return t1.IsGenericType && t2.IsGenericType && t1.GetGenericTypeDefinition() == t2.GetGenericTypeDefinition();
}

private static bool TryDecorateOpenGeneric(this IServiceCollection services, Type serviceType, Type decoratorType, [NotNullWhen(false)] out Exception? error)
{
var arguments = services
var closedGenericServiceTypes = services
.Where(x => !x.ServiceType.IsGenericTypeDefinition)
.Where(x => IsSameGenericType(x.ServiceType, serviceType))
.Select(x => x.ServiceType.GenericTypeArguments)
.ToArray();
.Where(x => HasSameTypeDefinition(x.ServiceType, serviceType))
.Select(x => x.ServiceType)
.Distinct()
.ToList();

if (arguments.Length == 0)
if (closedGenericServiceTypes.Count == 0)
{
error = new MissingTypeRegistrationException(serviceType);
return false;
}

foreach (var argument in arguments)
foreach (var closedGenericServiceType in closedGenericServiceTypes)
{
var closedServiceType = serviceType.MakeGenericType(argument);
var closedDecoratorType = decoratorType.MakeGenericType(argument);
var arguments = closedGenericServiceType.GenericTypeArguments;

var closedServiceType = serviceType.MakeGenericType(arguments);
var closedDecoratorType = decoratorType.MakeGenericType(arguments);

if (!services.TryDecorateDescriptors(closedServiceType, out error, x => x.Decorate(closedDecoratorType)))
{
Expand Down

0 comments on commit fe01bc6

Please sign in to comment.