流浪のwolf

卷帝

导航

C# Webapi 简单的依赖注入-构造函数

控制器部分:

using Microsoft.AspNetCore.Mvc;
using WebApplication1.IServices;
using WebApplication1.Utility.SwaggerExt;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    // 指定当前的控制器是哪个Swagger版本的  指定给 后台_version02
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.endApi))]
    public class LoginController : ControllerBase
    {
        private readonly Calc calc;
        public LoginController(Calc calc)
        {
            this.calc = calc;
        }
        [HttpGet]
        public int TestAdd(int i , int j)
        {
            return calc.Add(i, j);
        }
    }
}

服务部分:通过构造函数注入服务

namespace WebApplication1
{
    public class Calc
    {
        public int Add(int i,int j)
        {
            return i + j;
        }
    }
}

 当有两个服务的时候,其中一个服务消耗较长的时间,那么另外一个服务即使没有使用到时间长的服务,但是速度依旧很慢 ;

eg:

控制器:

using Microsoft.AspNetCore.Mvc;
using WebApplication1.IServices;
using WebApplication1.Utility.SwaggerExt;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    // 指定当前的控制器是哪个Swagger版本的  指定给 后台_version02
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.endApi))]
    public class LoginController : ControllerBase
    {
        private readonly Calc calc;
        private readonly TestService testService;
        public LoginController(Calc calc, TestService testService)
        {
            this.calc = calc;
            this.testService = testService;
        }
        /// <summary>
        /// 求和服务
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        [HttpGet]
        public int TestAdd(int i , int j)
        {
            return calc.Add(i, j);
        }
        /// <summary>
        /// 读取文件的服务 消耗时间长
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public int Test1()
        {
            return testService.Count;
        }
    }
}

求和服务:

namespace WebApplication1
{
    public class Calc
    {
        public int Add(int i,int j)
        {
            return i + j;
        }
    }
}

计算文件中 exe 个数的服务:

namespace WebApplication1
{
    public class TestService
    {
        private string[] files;
        public TestService()
        {
            this.files = Directory.GetFiles("C:/Users/朱龙旭/Downloads", "*.exe",
                SearchOption.AllDirectories);
        }
        // 计算exe文件的个数
        public int Count
        {
            get
            {
                return this.files.Length;
            }
        }
    }
}

 解决办法:通过 action 参数注入 ;

[FromServices] 
using Microsoft.AspNetCore.Mvc;
using WebApplication1.IServices;
using WebApplication1.Utility.SwaggerExt;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    // 指定当前的控制器是哪个Swagger版本的  指定给 后台_version02
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.endApi))]
    public class LoginController : ControllerBase
    {
        private readonly Calc calc;
        public LoginController(Calc calc)
        {
            this.calc = calc;
        }
        /// <summary>
        /// 求和服务
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        [HttpGet]
        public int TestAdd(int i , int j)
        {
            return calc.Add(i, j);
        }
        /// <summary>
        /// 读取文件的服务 消耗时间长
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Test1([FromServices] TestService testService,string kk)
        {
            return testService.Count.ToString() + "***" + kk;
        }
    }
}
 public string Test1([FromServices] TestService testService,string kk)
{
   return testService.Count.ToString() + "***" + kk;
}

posted on 2023-10-12 19:23  流浪のwolf  阅读(31)  评论(0编辑  收藏  举报