ASP. NET core development-Get all injection (DI) services

Source: Internet
Author: User

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 classStartup {Privateiservicecollection _services; //This method gets called by the runtime.        Use this method to add services to the container. //For more information on how to configure your application, visithttp://go.microsoft.com/fwlink/?LinkID=398940         Public voidconfigureservices (iservicecollection services) {_services=services; }        //This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory.            Addconsole (); var_logger = Loggerfactory.createlogger ("Services"); _logger. Loginformation ($"Total Services registered: {_services. Count}"); foreach(varServiceinch_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) =            {                awaitContext. 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 voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory.            Addconsole (); var_logger = Loggerfactory.createlogger ("Services"); _logger. Loginformation ($"Total Services registered: {_services. Count}"); foreach(varServiceinch_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 (AsyncContext ={context. Response.ContentType="text/html; Charset=utf-8"; awaitContext. Response.writeasync ($""); foreach(varSvcinch_services) {                        awaitContext. Response.writeasync ("<tr>"); awaitContext. Response.writeasync ($"<td>{svc. Servicetype.fullname}</td>"); awaitContext. Response.writeasync ($"<td>{svc. Lifetime}</td>"); awaitContext. Response.writeasync ($"<td>{svc. Implementationtype?. Fullname}</td>"); awaitContext. Response.writeasync ("</tr>"); }                    awaitContext. Response.writeasync ("</tbody></table>");                })); App.            Usedeveloperexceptionpage (); } app. Run (Async(context) =            {                awaitContext. Response.writeasync ("Hello world!");        }); }

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.

If you think this article is helpful to you, please click " recommend ", thank you.

ASP. NET core development-Get all injection (DI) services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.