Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core resolve dependency manually anywhere in the code

Do you know how to manually resolve dependencies in .net core? Something like

DependencyResolver.Resolve<ISomeService>()

UPDATE I'm in a class that was not injected, I want to resolve it from the inside, rather than pass variables all over the place

like image 793
SexyMF Avatar asked Jun 03 '18 08:06

SexyMF


People also ask

When to resolve dependency injection in ASP NET Core?

In ASP.NET Core dependency injection, we usually register injectable dependencies at the start of our program. We can then resolve these dependencies (services) much later once when we need them. Sometimes, however, we may need to resolve those dependencies even before building our app.

How to resolve instances with ASP NET Core di in static classes?

to be able to resolve instances with ASP.NET Core DI in static classes, you need a full “service provider” that contains the services you already have added from your startup.cs class. Isn’t enough create a new instance of IServiceProvider and use it.

How do I Resolve dependencies at runtime?

There's an easy way to resolve dependencies at runtime with our old friend, the factory pattern. Let's say we are writing a simple app to determine the weather for a location, determined by a postal code the user enters into a text box.

Is it possible to resolve instances from within configureservices?

Does this answer your question? Resolving instances with ASP.NET Core DI from within ConfigureServices Just be aware that you're creating a maintenance nightmare, as you can no longer trust the dependencies injected via the constructor to be all the dependencies.


1 Answers

Add your dependency in ConfigureServices as below

public void ConfigureServices(IServiceCollection services){
    //AddSingleton or AddTransient based on your requirements
    services.AddTransient<ISomeService, ConcreteService>();
}

In your controller or anywhere, add IServiceProvider in the constructor like below:

using Microsoft.Extensions.DependencyInjection;

...

public class HomeController
{
  ...
  public HomeController(IServiceProvider serviceProvider)
  {
      var service = serviceProvider.GetService<ISomeService>();
  }
}

@Shazam, Here are some notes or suggestions based on your comment:

  • If you can not inject because you might not have a constructor in this class, I woud suggest to add a paramter to your function and pass the resolved dependency from outside

  • Another Idea is to add a static property and initialize its value in ConfigureServices

For Example:

public static class MyClass
{
    public static ISomeService MyServiceObj { set; get; }
    ....
}

In your ConfigureServices

   public void ConfigureServices(IServiceCollection services){
        services.AddTransient<ISomeService, ConcreteService>();
        MyClass.MyServiceObj = services.GetService<ISomeService>();
    }

Hope this helps, please rate my answer or leave me a comment if you still in doubt how to do it

like image 83
Muhammad Soliman Avatar answered Oct 07 '22 06:10

Muhammad Soliman