Scrutor
Scrutor copied to clipboard
Duplicates when Scanning Assemblies with latest .NET 7 SDK versions
Hi, We are using Scrutor as part of our .NET Core Web Api implementation. The following code is used to scan dependencies of the api and register implementations of the handler interfaces:
internal static IServiceCollection AutoRegisterHandlers(this IServiceCollection services) { services.Scan(scan => scan.FromApplicationDependencies() .AddClasses(classes => classes.AssignableTo<IHandleGetRequests>()).As<IHandleGetRequests>() .AddClasses(classes => classes.AssignableTo<IHandleDeleteRequests>()).As<IHandleDeleteRequests>() .AddClasses(classes => classes.AssignableTo(typeof(IHandlePatchRequests<>))).AsImplementedInterfaces() .AddClasses(classes => classes.AssignableTo(typeof(IHandlePutRequests<>))).AsImplementedInterfaces() .AddClasses(classes => classes.AssignableTo(typeof(IHandlePostRequests<>))).AsImplementedInterfaces()); return services; }
With version 7.0.100 of the .NET SDK the above code works as expected and the results of the call to services.Scan include one entry for each implementation of the interfaces. However after upgrading the SDK to 7.0.2** (e.g. the latest version 7.0.203) then the assembly scanning produces duplicates for each interface, this in turn creates duplicates in our registration of the handlers and results in Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints
errors at runtime.
We are aware of the RegistrationStrategy stuff and we are looking at amending our code to make use of this with either Skip or Replace but wanted to check if you are aware of the issue and whether there are any plans to release an update of Scrutor in response to this or not?
Hi @markreed-hearst! 👋🏻
Haven't heard about this before. It's probably worth raising this with Microsoft if you think they broke something. Otherwise we'd probably need more details or a repro 😅
Here is a repro: https://github.com/markreed-hearst/Scrutor-200 When this is built with version 7.0.100 of the .NET SDK you'll get "My service collection has 1 entries." in the output. When built with e.g. version 7.0.203 of the .NET SDK you'll get "My service collection has 2 entries." they'll both be for the single ISayHelloToTheWorld interface implementation in the Library.
Used .UsingRegistrationStrategy(RegistrationStrategy.Skip)
as a workaround
Skip didn't work for me because it only filters on the ServiceType of the descriptor, so filters too many things out. I I write a similar custom strategy that also filter on ImplementationType then I get the effect I desire (I work with mark up above)
internal class UniqueStrategy : RegistrationStrategy
{
public override void Apply(IServiceCollection services, ServiceDescriptor descriptor)
{
if(!services.Any(x=>x.ServiceType == descriptor.ServiceType && x.ImplementationType == descriptor.ImplementationType))
{
services.Add(descriptor);
}
}
}
I wonder if this is specific to FromApplicationDependencies
and multiple references to the same assembly, possibly transitively?
Maybe, but Mark's repro repo doesn't seem to have multiple references to the same assembly.