question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AsImplementedInterfaces() registering too much

See original GitHub issue

I 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:open
  • Created 5 years ago
  • Reactions:6
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
khellangcommented, Mar 2, 2022

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,

1reaction
khellangcommented, Mar 1, 2022

I do not actually need to register IRegisteredObject but it also seems like there is no way to tell Scrutor to ignore this interface.

@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 😎

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found