zoukankan      html  css  js  c++  java
  • netcore2.2升级到3.1依赖注入插件切换

    问题描述:原来在2.2版本中使用autofac作为注入时的管理容器,现在要升级到3.1版本,遇到了很多的问题,各种解析不了。。。

    解决方案:最后因为水平太low就放弃了,改用微软自带的容器管理,改造过程中也遇到了一些问题

    1、注册顺序需要注意,尤其是有依赖关系的;还有就是注册类型,静态的不能引用会话的,说的有点含糊了。。。

    2、过滤器中属性注入,之前是使用的静态类,直接解析就ok,现在要变成构造方法中注入,控制器调用时莫非要把参数都带上?不用的,这个时候需要使用ServiceFilter

            [ServiceFilter(typeof(HandlerAuthorizeAttribute))]
            public ActionResult Index()
            {
                return View();
            }

    Startup类中主要修改方法如下

     public void ConfigureServices(IServiceCollection services)
            {
                //Redis服务帮助类(单例)
                services.AddSingleton<RedisHelper>();
                //使用Redis缓存
                services.AddSingleton<ICacheContext, CacheContextByRedis>();
                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                //注册基础服务
                services.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
                services.AddScoped<IUnitWork, UnitWork>();
                services.AddScoped<IAuth, LocalAuth>();
                services.AddScoped<LoginParse>();
                services.AddSingleton<VerifyCode>();
    
                //注册App(注意引用顺序)
                services.AddScoped<SysLogApp>();
                services.AddScoped<CreateStaticApp>();
                services.AddScoped<NewsClassApp>();
                services.AddScoped<NewsDetailApp>();
                services.AddScoped<FriendshipLinkApp>();
                services.AddScoped<ScrollPicApp>();
                services.AddScoped<SysModuleApp>();
                services.AddScoped<SysModuleButtonApp>();
                services.AddScoped<SysRoleAuthorizeApp>();
                services.AddScoped<SysRoleApp>();
                services.AddScoped<SysUserApp>();
    
                //注册过滤器
                services.AddScoped<HandlerAuthorizeAttribute>();
                services.AddScoped<HandlerLoginAttribute>();
                services.AddScoped<HandlerLoginForAppAttribute>();
    
                services.AddMvc(opt => {
                    opt.Filters.Add<HandlerExceptionAttribute>();
                });
                services.AddMemoryCache();
                services.AddOptions();
                services.Configure<AppSetting>(Configuration.GetSection("AppSettings"));
                services.AddDbContext<XXXDBContext>(options =>
                    options.UseMySql(Configuration.GetConnectionString("MySQL")));
            }
  • 相关阅读:
    树莓派4B 多屏 QT程序窗口全屏 QScreen 只能获取1个屏幕
    树莓派4B 微雪7寸触摸屏 双屏 触摸屏校正
    虚拟机 ubuntu18 树莓派4 QT5.14.2 交叉编译
    Qt 指定 so库 运行时路径
    building qtqml requires python
    python django 测试报告 发送邮件
    jmeter XPath Extractor
    python+unittest+HTMLTestRunner生成测试报告
    Genymotion、 uiautomatorviewer、 appium报错
    jmeter forEach控制器
  • 原文地址:https://www.cnblogs.com/wangbg/p/12237202.html
Copyright © 2011-2022 走看看