2

Is it possible to configure ASP.NET Core DI to resolve all classes? Something similar to Autofac AnyConcreteTypeNotAlreadyRegisteredSource functionality.

I'm able to use Autofac in ASP.NET Core, but I do not want to add additional library to my application, because ASP.NET Core already has DI.

So basically instead of:

services.AddTransient<MyService>();
services.AddTransient<MyAnotherService>();

I'd prefer to do something like:

services.ResolveAll();
Maroun
  • 91,013
  • 29
  • 181
  • 233
Vadim Sentiaev
  • 683
  • 4
  • 18
  • 1
    I suppose it could be done but there isn't already a convenient call to do it. I'm curious about what your particular use case is for this. What are you trying to accomplish and why do you think this is the best way to do it? – Erik Noren Mar 24 '17 at 06:33
  • @ErikNoren it's how I usually use DI. I prefer do not create silly interfaces. So if I need only one implementation I will create a class instead of interface and class (to make test frameworks or DI frameworks happy). And almost all projects do not need several implementations for some functionality. So, if I want to inject some `Foo` class it's only `Foo` class I have. And my goal is to not configure DI at all for this. Actually I use Autofac for this `builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());` – Vadim Sentiaev Jul 22 '17 at 23:31

3 Answers3

8

it seems we can start down this general automatic injection concept with a little reflection

i've filtered on "Provider" namespace and look for class names spelled the same as interfaces without "I", but of course season to your own taste

(this code works under an asp.net core 2.0 context)

  var allProviderTypes = System.Reflection.Assembly.GetExecutingAssembly()
    .GetTypes().Where(t=>t.Namespace != null && t.Namespace.Contains("Providers"));

  foreach(var intfc in allProviderTypes.Where(t=>t.IsInterface)) {
    var impl = allProviderTypes.FirstOrDefault(c=>c.IsClass && intfc.Name.Substring(1) == c.Name);
    if (impl != null) services.AddScoped(intfc, impl);
  }
Bernoulli IT
  • 4,795
  • 4
  • 36
  • 55
Beej
  • 714
  • 7
  • 13
  • thanks for sharing this, but question was about some build in method. I do not check if it's possible in 2.0, but looks like it's not. – Vadim Sentiaev Dec 09 '17 at 23:59
  • 1
    Perhaps something like: var impl = allProviderTypes.FirstOrDefault(c=>c.IsClass && intfc.IsAssignableFrom(c) && !c.IsAbstract); would be a lot less brittle than assuming a naming convention. – computrius Mar 27 '20 at 18:43
6

Perhaps Scrutor (Scanning feature) is close to what you`re looking for. https://github.com/khellang/Scrutor

3

ASP.NET Core DI doesn't support auto-discovery and auto-registration and there is no plan to add those features (at least in near feature).

The main reason is that the .NET Core team tries to keep the built-in container as simple as possible and saves an easy/straitforward way for adding other DI containers.

Related SO question: Property Injection in Asp.Net Core

Community
  • 1
  • 1
Set
  • 44,147
  • 19
  • 125
  • 140
  • That's not strictly true. You can scan and register dependencies found in assemblies using Scrutor. Check out IServiceCollection Scan. – Keith Hill Jul 16 '20 at 19:31