本文介绍了我应该使用哪个PreApplicationStartMethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当我从NuGet将StructureMap安装到我的ASP.NET MVC3项目中时,Dave Ebbo的 WebActivator 软件包也被添加为依赖项.

I noticed that when I installed StructureMap from NuGet into my ASP.NET MVC3 project, Dave Ebbo's WebActivator package was also added as a dependency.

WebActivator提供一个PreApplicationStartMethod属性,并且在安装时添加的样板代码中,它用于初始化其自身类中的IoC容器和依赖项解析器,而不是在Global.asaxApplication_Start中进行此操作方法.

WebActivator provides a PreApplicationStartMethod attribute and, in the boilerplate code added at install time, it is used to initialise the IoC container and dependency resolver in it's own class, instead of doing this inside Global.asax's Application_Start method.

鉴于ASP.NET 4已经拥有自己的 为什么WebActivator有必要提供其自己的版本,然后让StructureMap使用该版本?

Given that ASP.NET 4 already has its own System.Web.PreApplicationStartMethodAttribute why was it necessary for WebActivator to supply its own version and for StructureMap to use that?

我猜我没有没有使用WebActivator的变体吗?

I am guessing I don't have to use WebActivator's variant?

为达林添加了代码:

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));
    }
  }
}

推荐答案

ASP.NET MVC 3中用于DI容器的NuGet软件包通常更喜欢使用WebActivator来避免弄乱您在Application_Start中可能存在的任何现有代码. Ninject使用完全相同的方法.

NuGet packages for DI containers in ASP.NET MVC 3 usually prefer to use WebActivator to avoid messing with any existing code that you might have in Application_Start. Ninject uses exactly the same approach.

您可以在应用程序中具有多个WebActivator.PreApplicationStartMethod属性,并且在.NET 4.5中先于单个System.Web.PreApplicationStartMethodAttribute.

You can have multiple WebActivator.PreApplicationStartMethod attributes in your application and prior to .NET 4.5 a single System.Web.PreApplicationStartMethodAttribute.

这篇关于我应该使用哪个PreApplicationStartMethod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 12:50