流浪のwolf

卷帝

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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   朱龙旭的网络  阅读(54)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示