Kev*_*Kev 9 structuremap asp.net asp.net-mvc
我注意到当我从NuGet将StructMap安装到我的ASP.NET MVC3项目中时,Dave Ebbo的WebActivator包也被添加为依赖项.
WebActivator提供了一个PreApplicationStartMethod属性,并且在安装时添加的样板代码中,它用于在其自己的类中初始化IoC容器和依赖项解析器,而不是在内部Global.asax的Application_Start方法中执行此操作.
鉴于ASP.NET 4已经有了自己的System.Web.PreApplicationStartMethodAttribute原因,为什么WebActivator必须提供自己的版本,而StructureMap才能使用它?
我猜我不具备使用WebActivator的变种?
为Darin添加了代码:
using System.Web;
using System.Web.Mvc;
using StructureMap;
[assembly: WebActivator.PreApplicationStartMethod(
typeof(MyMvcApp.App_Start.StructuremapMvc), "Start")]
// or
[assembly: PreApplicationStartMethod(
typeof(MyMvcApp.App_Start.StructuremapMvc), "Start")]
namespace MyMvcApp.App_Start {
public static class StructuremapMvc {
public static void Start() {
var container = (IContainer) IoC.Initialize();
DependencyResolver.SetResolver(new SmDependencyResolver(container));
}
}
}
Run Code Online (Sandbox Code Playgroud)
ASP.NET MVC 3中的DI容器的NuGet包通常更喜欢使用WebActivator来避免弄乱您可能拥有的任何现有代码Application_Start.Ninject使用完全相同的方法.
您可以WebActivator.PreApplicationStartMethod在应用程序中和.NET 4.5 之前使用多个属性System.Web.PreApplicationStartMethodAttribute.