Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicates when Scanning Assemblies with latest .NET 7 SDK versions #200

Open
markreed-hearst opened this issue Apr 21, 2023 · 6 comments · May be fixed by #209
Open

Duplicates when Scanning Assemblies with latest .NET 7 SDK versions #200

markreed-hearst opened this issue Apr 21, 2023 · 6 comments · May be fixed by #209

Comments

@markreed-hearst
Copy link

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?

@khellang
Copy link
Owner

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 😅

@markreed-hearst
Copy link
Author

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.

@egorshulga
Copy link

Used .UsingRegistrationStrategy(RegistrationStrategy.Skip) as a workaround

@ChristopherMeek
Copy link

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

@khellang
Copy link
Owner

I wonder if this is specific to FromApplicationDependencies and multiple references to the same assembly, possibly transitively?

@ChristopherMeek
Copy link

Maybe, but Mark's repro repo doesn't seem to have multiple references to the same assembly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants