NET Core development-Get all injection (DI) services
Gets all the injection (DI) services in ASP. Dependency Injection Dependency Injection in ASP.
We use injected services in the controller, or elsewhere in the ASP. NET core program, such as logging.
How do we get all the injection (DI) services in ASP., let's look at what the ASP. NET core injects.
A simple introduction to Dependency injection:
Dependency Injection (Dependency injection, DI) is a technique that implements loose coupling between an object and its collaborators or dependencies. These objects that the class uses to perform its operations are provided to the class in some way, rather than instantiating collaborators directly or using static references.
We create a new ASP. NET Core empty application.
Then the startup defines a variable private iservicecollection _services;
public class Startup {private Iservicecollection _services; This method gets called by the runtime. Use this method to add services to the container. For more information on what to configure your application, visit http://go.microsoft.com/fwlink/? linkid=398940 public void Configureservices (iservicecollection services) {_services = Services; }//This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { Loggerfactory.addconsole (); var _logger = Loggerfactory.createlogger ("Services"); _logger. Loginformation ($ "Total Services registered: {_services. Count} "); foreach (var service in _services) {_logger. Loginformation ($ "service: {service. servicetype.fullname}\n Lifetime: {service. Lifetime}\N Instance: {service. Implementationtype?. FullName} "); } if (env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } app. Run (Async (context) = {await context. Response.writeasync ("Hello world!"); }); } }
Print it out using logger in Configure.
To execute the program, you can see that there are 14 services by default.
Then we can also display these services on the Web page.
We add the following code to the Configure:
public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { Loggerfactory.addconsole (); var _logger = Loggerfactory.createlogger ("Services"); _logger. Loginformation ($ "Total Services registered: {_services. Count} "); foreach (var service in _services) {_logger. Loginformation ($ "service: {service. servicetype.fullname}\n Lifetime: {service. lifetime}\n Instance: {service. Implementationtype?. FullName} "); } if (env. Isdevelopment ()) {app. Map ("/allservices", builder = Builder. Run (Async context = {context. Response.ContentType = "text/html; Charset=utf-8 "; Await the context. Response.writeasync ($ "Then we use the self-hosted way to execute, enter http://localhost:5000/allservices in the address bar
Also can see 14 services, here I put/allservices in development environment, only in the program debugging can see, run is not.
We add a Microsoft.AspNetCore.Mvc.Core reference to the project.
And then join in the configureservices.
public void Configureservices (iservicecollection services) { services. Addmvccore ();//Add mvccore _services = Services; }
We can see the extra service Mvccore.
Http://localhost:5000/allservices
You can see that a lot of services are added. We can also add others and see which services are injected.
NET Core development-Get all injection (DI) services