.Net Core 学习依赖注入自定义Service

1. 定义一个服务,包含一个方法

public class TextService
{
    public string Print(string m)
    {
        return m;
    }
}

2. 写一个扩展方法用来注入服务

using Haos.Develop.CoreTest.Service;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Haos.Develop.CoreTest
{
    public static class Extension
    {
        public static IServiceCollection AddTestService(this IServiceCollection service)
        {
            return service.AddScoped(factory => new TextService());
        }
    }
}

3. 回到Startup类中找到ConfigureServices方法添加如下代码

// 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, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddTestService();//手动高亮
}

4.我们可以采用构造函数方式来使用或者方法用参数的形式注入和直接获取

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Haos.Develop.CoreTest.Service;

namespace Haos.Develop.CoreTest.Controllers
{
   
    public class HomeController : Controller
    {
        public TextService T;

        public HomeController(TextService t)
        {
            T = t;
        }

        public ActionResult Index()
        {
            return Content("ok");
        }
        /// <summary>
        /// 使用构造函数注入
        /// </summary>
        /// <returns></returns>
        public JsonResult Test()
        {
            T.Print("哈哈哈哈哈哈哈哈哈哈哈哈");
            return Json("");
        }
        /// <summary>
        /// 参数注入
        /// </summary>
        /// <param name="t2"></param>
        /// <returns></returns>
        public JsonResult Test2(TextService t2)
        {
            t2.Print("哈哈哈哈哈哈哈哈哈哈哈哈");
            return Json("");
        }
        /// <summary>
        /// 直接获取
        /// </summary>
        /// <returns></returns>
        public JsonResult Test3()
        {
            var t3 = HttpContext.RequestServices.GetService(typeof(TextService)) as TextService;
            t3.Print("哈哈哈哈哈哈哈哈哈哈哈哈");
            return Json("");
        }
    }
}

5. 如果存在参数可以在构造函数中赋值

示例:

5.1 修改第一点的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Haos.Develop.CoreTest.Service
{
    public class TextService
    {
        public string StrString { get; set; }

        public TextService(string m)
        {
            StrString = m;
        }

        public string Print(string m)
        {
            return StrString + m;
        }
    }
}

5.2 修改第二点的代码

using Haos.Develop.CoreTest.Service;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Haos.Develop.CoreTest
{
    public static class Extension
    {
        public static IServiceCollection AddTestService(this IServiceCollection service,string str)
        {
            return service.AddScoped(factory => new TextService(str));
        }
    }
}

最后注入

 public void ConfigureServices(IServiceCollection services)
 {
    services.AddTestService("this test param");
 }

 6 生命周期

6.1 瞬时(Transient)

生命周期服务在它们每次请求时被创建。这一生命周期适合轻量级的,无状态的服务。

6.2 作用域(Scoped)

作用域生命周期服务在每次请求被创建一次。

6.3 单例(Singleton)

单例生命周期服务在它们第一次被请求时创建并且每个后续请求将使用相同的实例。

posted @ 2017-10-30 13:41  浩叔  阅读(7606)  评论(1编辑  收藏  举报