ASP.NET 5 Dependency Injection with Autofac
来源:互联网 发布:淘宝买iphone8靠谱吗 编辑:程序博客网 时间:2024/06/02 02:31
Default Dependency Injection container
First, let’s see what is shipped with ASP.NET 5 by default. There is already simple DI container. It gives you a possibility to register service with three different lifetimes: scope, singleton and transient. For now it supports only constructor injection. Let’s see how to use it and what is the difference between these lifetimes.
Create empty ASP.NET 5 project. Create simple service interface IService.cs:
public interface IService{ int GetCounter();}
and implementation SimpleService.cs:
public class SimpleService : IService{ private int counter; public int GetCounter() { return counter++; }}Now create controller to test our service ServiceController.cs:
public class ServiceController : Controller{ private readonly IService service; public ServiceController(IService service) { this.service = service; } public int Get() { return service.GetCounter(); }}It is time to register our service in DI container, open Startup.cs and edit:
Singleton
public void ConfigureServices(IServiceCollection services){ services.AddMvc(); services.AddSingleton<IService, SimpleService>();}Start your application and go to http://localhost/service/get, as you can see, number is increasing every time you reload the page, it is because our service was created when we first-time requested API method and all further requests will use the same instance of service.
Scope
Transient
Change registration code as below:
services.AddScoped<IService, SimpleService>();
Now, after each page reload you will see ‘0’. It is because service is creating per each request.
Transient
Change registration code:
services.AddTransient<IService, SimpleService>();
An output will be the same as in previous example. An object registered as Transient will be instantiated every time it is requested. To see this difference let’s modify our controller’s constructor:
public ServiceController(IService service, IService service2){ service2.GetCounter(); this.service = service;}Now it takes two services. In case of Transient registration, we will have two different instances, but if you specify Scope lifetime you will have the same instance in both parameters.
Use third-party DI containers (Autofac)
You can also use third-party dependency injection containers with ASP.NET 5. I will show you how to do that with Autofac. Why Autofac? It already has support for DNX Core and APS.NET 5.
First modify project.json, add Autofac in dependency block:
"Autofac": "4.0.0.0-alpha2","Autofac.Dnx": "4.0.0-alpha1"
Now, create Autofac module AutofacModule.cs:
public class AutofacModule : Module{ protected override void Load(ContainerBuilder builder) { builder .Register(c => new SimpleService()) .As<IService>() .InstancePerLifetimeScope(); }}
Modify Startup.cs:
public IServiceProvider ConfigureServices(IServiceCollection services){ services.AddMvc(); // Create the Autofac container builder. var builder = new ContainerBuilder(); // Add any Autofac modules or registrations. builder.RegisterModule(new AutofacModule()); // Populate the services. builder.Populate(services); // Build the container. var container = builder.Build(); // Resolve and return the service provider. return container.Resolve<IServiceProvider>();}
Do not forget to change ConfigureServices signature, it should return IServiceProvider. Start you app and you will see the same result as Scope example.
0 0
- ASP.NET 5 Dependency Injection with Autofac
- Dependency Injection with Autofac
- Dependency injection with Flex
- Introduction to dependency injection with unityContainer
- Dependency Injection
- Dependency Injection
- Dependency Injection
- Dependency Injection
- Ioc容器Autofac asp.net mvc中整合autofac
- Inversion of Control and Dependency Injection with Castle Windsor Container
- 【.Net码农】【Autofac】Ioc容器Autofac系列(2)-- asp.net mvc中整合autofac
- ASP.NET MVC IOC 之AutoFac攻略
- ASP.NET MVC IOC 之AutoFac攻略
- ASP.NET MVC IOC 之AutoFac攻略
- ASP.NET MVC IOC 之AutoFac攻略
- ASP.NET MVC IOC 之AutoFac
- ASP.NET MVC IOC 之AutoFac攻略
- ASP.NET MVC 之 IOC AutoFac攻略
- android:自定义温度计View
- greenplum安装
- CDISC SDTM MI domain学习笔记
- php 使用ajax文本框模糊查询
- “J.U.C”:Phaser (r)
- ASP.NET 5 Dependency Injection with Autofac
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- angular-按需加载angularjs和$http.post的二三事
- iOS App Crash日志
- 解决了一个连接mysql特别慢的问题
- 时间日期类详解--Calendar
- BZOJ 1057 [ZJOI2007]棋盘制作
- kvc与kvo
- SpiderMonkey-让你的C++程序支持JavaScript脚本