[. Net Core] simple use of the built-in Mvc Ioc (continued), mvcioc

Source: Internet
Author: User

[. Net Core] simple use of the built-in Mvc Ioc (continued), mvcioc
Simple use of built-in Mvc Ioc (continued)

This article is based on. NET Core 2.0.

The previous chapter [. Net Core] simple use of built-in Mvc Ioc has explained the simple usage of Ioc in daily Mvc. In addition, there are some content to be supplemented.

Next we will answer these questions: What is the combination of Filter and Ioc in AOP? How can I directly retrieve instance objects in Ioc, rather than using Constructors?

 

Directory
  • Scenario 1: Use Ioc with Filter
  • Scenario 2: directly obtain the object managed by Ioc

 

Scenario 1: Use Ioc with Filter

First, write a filter ExceptionFilter. cs, which inherits the IExceptionFilter and is mainly used to record logs when errors occur. The namespace used is Microsoft. AspNetCore. Mvc. Filters.

using Microsoft.AspNetCore.Mvc.Filters;namespace IocCoreDemo{    public class ExceptionFilter : IExceptionFilter    {        private readonly LogService _logService;        public ExceptionFilter(LogService logService)        {            _logService = logService;        }        public void OnException(ExceptionContext context)        {            _logService.Error(context.Exception.Message);        }    }}

 

LogService. cs:

    public class LogService    {        public void Error(string message)        {            Console.WriteLine(message);        }    }

 

I explicitly asked the Controller Demo4Controller. cs to throw an unimplemented exception in the Index () method for testing:

    public class Demo4Controller : Controller    {        public IActionResult Index()        {            throw new NotImplementedException();        }    }

 

Next, we will modify the code in the ConfigureServices () method of the core Startup. cs. The editor uses AddSingleton () as the example Method for injection:

Public void ConfigureServices (IServiceCollection services) {services. addSingleton (typeof (ExceptionFilter); // inject ExceptionFilter services. addSingleton (typeof (LogService); // inject LogService services. addMvc (x => {// create a ServiceProvider object var provider = services. buildServiceProvider (); var filter = provider. getService <ExceptionFilter> (); // Add filter x. filters. add (filter );});}

 

Public class Demo5Service {public string Test () {return Guid. NewGuid (). ToString ();}}

 

Method 1

We can use this. HttpContext. RequestServices. GetService () in the Controller to obtain the service objects injected into Ioc, such as the controller Demo5Controller. cs:

    public class Demo5Controller : Controller    {        public IActionResult Index()        {            var demoService = (Demo5Service) this.HttpContext.RequestServices.GetService(typeof(Demo5Service));            return Json(demoService.Test());        }    }

 

Do not forget to inject the service class into Startup. cs:

Public class Startup {public static ServiceProvider {get; private set ;}// This method gets called by the runtime. use this method to add services to the container. public void ConfigureServices (IServiceCollection services) {services. addMvc (); services. addSingleton <Demo5Service> (); ServiceProvider = services. buildServiceProvider ();}//...}

 

Public class Demo5Controller: Controller {public IActionResult Index () {var demoService = (Demo5Service) Startup. serviceProvider. getService (typeof (Demo5Service); return Json (demoService. test ());}}

 

 

Original article: http://www.cnblogs.com/liqingwen/p/8585119.html

Related Articles:

[. Net Core] simple reading of json configuration files

[. Net Core] simple use of built-in Mvc Ioc

[. Net Core] simple use of built-in Mvc Ioc (continued)

Related Article

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.