我应该使用哪个PreApplicationStartMethod?

编程入门 行业动态 更新时间:2024-10-27 16:27:28
本文介绍了我应该使用哪个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.asax的Application_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?

本文发布于:2023-11-09 05:15:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PreApplicationStartMethod

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!