【发布时间】:2017-04-10 02:54:51
【问题描述】:
我有一个包含 WebAPI 和 MVC 控制器的 C# 项目,我想使用 Simple Injector。 https://simpleinjector.readthedocs.io/en/latest/mvcintegration.html 的 Simple Injector 文档将 MVC 的 Application_Start 方法显示为
protected void Application_Start(object sender, EventArgs e) {
// Create the container as usual.
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
// Register your types, for instance:
container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);
// This is an extension method from the integration package.
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
但https://simpleinjector.readthedocs.io/en/latest/webapiintegration.html 显示以下 WebAPI
protected void Application_Start() {
// Create the container as usual.
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
// Register your types, for instance using the scoped lifestyle:
container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
// Here your usual Web API configuration stuff.
}
请问如何编写 Application_Start 代码,以便在 WebAPI 和 MVC 控制器中使用 Simple Injector?
【问题讨论】:
标签: asp.net-mvc asp.net-web-api simple-injector