Autofac Dependency Injection

Simple to understand: https://www.cnblogs.com/WeiGe/p/3871451.html

 Item code: GitHub

owin + webapi used Autofac:

Add Nuget package:

Autofac 
Autofac.WebApi2.Owin

 

owin added:

 //注册依赖注入
            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);

 

① write logic code in the Repository warehousing, Service application code that calls warehousing

Column such as:

 

 

 

 

 

②webapi in

 

Guess you like

Origin www.cnblogs.com/Sea1ee/p/10980025.html