Which PreApplicationStartMethod should I use?
Asked Answered
S

1

9

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 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.

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?

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

Added code for 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));
    }
  }
}
Swanner answered 24/1, 2012 at 12:55 Comment(0)
R
7

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.

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

Rowan answered 24/1, 2012 at 12:59 Comment(9)
I totally get that, but I'm curious as to why StructureMap prefers to use the WebActivator.PreApplicationStartMethodAttribute over the ASP.NET 4 provided System.Web.PreApplicationStartMethodAttribute.Swanner
@Kev, that's because you can have a single System.Web.PreApplicationStartMethodAttribute per application and if StructureMap had used it, you would no longer be able to provide your own custom initializations. The WebActivator.PreApplicationStartMethod is more elaborate than the built-in ASP.NET 4.0 attribute. It relies on it but it uses reflection to fetch all the WebActivator.PreApplicationStartMethod being registered and execute all of them. That's why NuGet packages use this approach. To avoid hijacking you the built-in method.Rowan
Aye, but forgive me if I'm sounding a bit dim, StructureMap doesn't actually use it as in reserving it so no other initialisation code can be added. See the code I added to my question. I could still add other initialisation code to the Start() that does other non-StructureMap work.Swanner
@Kev, yes, you can add other code. It's just that the class that was generated for you by the NuGet was intended to setup StructureMap. If you wanted to add custom code you could have written another class and use the WebActivator.PreApplicationStartMethod to subscribe to it. Personally I would use a separate initialization class for other application initialization tasks. I thought that your question was why StructureMap NuGet used WebActivator's method instead of the ASP.NET 4.0 built-in one.Rowan
Ah, sorry Darin, stupid me didn't read your update properly and missed the the word multiple :/. Got it now.Swanner
It's not true that you can have only one System.Web.PreApplicationStartMethodAttribute per application... only one per assembly.Rupee
@JeffPutz Are you sure that is true? I just tested it and there seems to be no problems in having multiple within the same Assembly.Ascribe
It's absolutely true. And I believe as of .Net 4.5, it's not even restricted to one per assembly anymore.Rupee
In 4.5 you can have multiple PreApplicationStartMethodAttribute within the same assembly. #11801454Cradling

© 2022 - 2024 — McMap. All rights reserved.