加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Autofac 依赖注入

发布时间:2020-12-14 04:49:58 所属栏目:百科 来源:网络整理
导读:简单了解:https://www.cnblogs.com/WeiGe/p/3871451.html ?项目代码:GitHub owin+webapi中使用Autofac: 添加Nuget包: Autofac Autofac.WebApi2.Owin ? owin中添加: // 注册依赖注入 ContainerBuilder builder = new ContainerBuilder(); // Register you

简单了解:https://www.cnblogs.com/WeiGe/p/3871451.html

?项目代码:GitHub

owin+webapi中使用Autofac:

添加Nuget包:

Autofac
Autofac.WebApi2.Owin

?

owin中添加:

 //注册依赖注入
            ContainerBuilder builder = new ContainerBuilder();
            // Register your Web API controllers.
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            
            builder.RegisterType<JwtUserRepository>().As<IJwtUserRepository>();
            builder.RegisterType<JwtUserService>().As<IJwtUserService>();

            // Run other optional steps,like registering filters,// per-controller-type services,etc.,then set the dependency resolver
            // to be Autofac.
            IContainer container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            // OWIN WEB API SETUP:

            // Register the Autofac middleware FIRST,then the Autofac Web API middleware,// and finally the standard Web API middleware.
            app.UseAutofacMiddleware(container);
            app.UseAutofacWebApi(config);

?

①在Repository?仓储中写逻辑代码,Service? 应用中调用仓储代码

列如:

?

?

?

?

?

②webapi中

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读