AsImplementedInterfaces() registering too much
See original GitHub issueI understand that AsImplementedInterfaces() registers all implemented interfaces, however this is sometimes too much. Take for instance, the FluentValidation library, you may scan with the following:
.AddClasses(c => c.AssignableTo(typeof(IValidator<>))).AsImplementedInterfaces()
This will pickup all the AbstractValidator<T>
implementations you have, but this class implements the following interfaces:
IValidator<T>
IEnumerable
IEnumerable<IValidationRule>
IValidator
Which means you get entries for all of these, not just the desired IValidator<T>
and I need to do the following to get rid of the superfluous entries:
services.RemoveAll<IValidator>();
services.RemoveAll<IEnumerable>();
services.RemoveAll<IEnumerable<IValidationRule>>();
Is there any way to do this without having to remove the extra entries afterwards?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Autofac registers components multiple times
Registered them in a newly created container and tried to resolve the "Test50" class directly from the container. As you already found out....
Read more >How to register a service with multiple interfaces in ASP. ...
In this post I describe how to register a class that implements multiple interfaces with the ASP.NET Core DI container, and how to...
Read more >Assembly Scanning — Autofac 7.0.0 documentation
AsImplementedInterfaces(). Register the type as providing all of its public interfaces as services (excluding IDisposable ). builder.RegisterAssemblyTypes(asm) ...
Read more >NET Core project without Autofac. Is it viable? - Alex Klaus
The first catch – lack of AsImplementedInterfaces() , a helper method to register the type as providing all of its public interfaces.
Read more >Dependency injection — Autofac
This is the basic registration of classes in autofac. var builder = new ContainerBuilder(); builder.RegisterType().As(); builder.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve added an overload to
AsImplementedInterfaces
that lets you filter the interfaces you’d like to register using a predicate and pushed it to NuGet as part of v4.1.0 😄 I’ll leave the issue open to track the breaking change of filtering out some common types by default,@THammond9 There is. It’s mentioned as a workaround in this issue.
I think a short-term non-breaking fix for this issue would be to introduce an overload of
AsImplementedInterfaces
that takes a filter predicate so people could filter out what they don’t want.If anyone wants to send a PR before I get to it, that would be cool 😎