ASP.NET Core DependencyResolver

In ASP.NET MVC 5, you can get some dependency through DependencyResolver.Current.GetService<T>() . Is there something similar in ASP.NET Core?

+5
source share
5 answers

Yes there is. In ASP.NET Core 1.0.0, the services available in a request from HttpContext are displayed through the RequestServices [1] collection:

 this.HttpContext.RequestServices 

You can use the GetService method to extract the dependencies by specifying the type of dependency:

 this.HttpContext.RequestServices.GetService(typeof(ISomeService)); 

Typically, you should not use these properties directly, preferring instead to query the types you require through the class constructor, and letting the infrastructure inject these dependencies. This gives classes that are easier to test and more loosely coupled.

[1] https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#request-services

+7
source

If you really need it, you can write your own. First create an AppDependencyResolver class.

 public class AppDependencyResolver { private static AppDependencyResolver _resolver; public static AppDependencyResolver Current { get { if (_resolver == null) throw new Exception("AppDependencyResolver not initialized. You should initialize it in Startup class"); return _resolver; } } public static void Init(IServiceProvider services) { _resolver = new AppDependencyResolver(services); } private readonly IServiceProvider _serviceProvider; public object GetService(Type serviceType) { return _serviceProvider.GetService(serviceType); } public T GetService<T>() { return _serviceProvider.GetService<T>(); } private AppDependencyResolver(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } } 

Note that _serviceProvider.GetService<T>(); Available only when added using Microsoft.Extensions.DependencyInjection; . This namespace will be available if you add "Microsoft.Extensions.DependencyInjection": "1.0.0" to your project.json . Then you must call the Init method in your startup class. for instance

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { AppDependencyResolver.Init(app.ApplicationServices); //all other code 

After that, you can use it anywhere, just like DependencyResolver.Current . But my suggestion is to use it only if there is no other choice.

+5
source

The service is available in the core of Asp.net, it is inside the HttpContext

 this.HttpContext.RequestServices 

Using this service, you can get the service. and also you can use the GetService method to retrieve the dependencies by specifying the type of dependency:

 this.HttpContext.RequestServices.GetService(typeof(ISomeService)); 
+2
source

There are extension methods for IServiceProvider: GetService, GetRequiredService and GetServices. All have common and non-common versions. In the Asp.Net Core web project, you can get a link to IServiceProvider through the DI or from IApplicationBuilder. In the console application, you must create your own instance of IServiceProvider and store the link somewhere

 var services = new ServiceCollection(); ... ServiceProvider = services.BuildServiceProvider(); 
+1
source

I think this may be a good start:

This is the official documentation for dependency injection for asp.net 5.

Dependency inclusion is now built into asp.net 5, but you can use other libraries like autofac. By default, it works fine.

In your starup class, you have a method like this

 public void ConfigureServices(IServiceCollection services) { //IServiceCollection acts like a container and you //can register your classes like this: services.AddTransient<IEmailSender, AuthMessageSender>(); services.Singleton<ISmsSender, AuthMessageSender>(); services.AddScoped<ICharacterRepository, CharacterRepository>(); } 

From:

DependencyResolver Asp.net 5.0

0
source

All Articles