简单了解: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  应用中调用仓储代码

列如:

Autofac 依赖注入

Autofac 依赖注入

 

 

 Autofac 依赖注入

Autofac 依赖注入

 

 

②webapi中

Autofac 依赖注入

 

相关文章:

  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2019-12-28
猜你喜欢
  • 2020-06-02
  • 2021-05-31
  • 2022-12-23
  • 2021-07-07
  • 2021-12-02
相关资源
相似解决方案