NET Core development-Get all injection (DI) services

Source: Internet
Author: User

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

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.