我注意到当我从NuGet安装StructureMap到我的ASP.NET MVC3项目中时,Dave Ebbo的WebActivator包也作为依赖项添加了进来。
WebActivator提供了一个
鉴于ASP.NET 4已经有了自己的
我猜想我不一定需要使用WebActivator的变体?
为Darin添加的代码:
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));
}
}
}
System.Web.PreApplicationStartMethodAttribute
,如果StructureMap使用它,您将无法提供自己的自定义初始化。WebActivator.PreApplicationStartMethod
比内置的ASP.NET 4.0属性更为复杂。它依赖于它,但它使用反射来获取所有正在注册的WebActivator.PreApplicationStartMethod
并执行它们中的所有方法。这就是为什么NuGet包使用此方法的原因。为了避免劫持内置方法。 - Darin Dimitrov