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));
    }
  }
}
Kev
  • 118,037
  • 53
  • 300
  • 385

1 Answers1

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.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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`. – Kev Jan 24 '12 at 13:01
  • 1
    @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. – Darin Dimitrov Jan 24 '12 at 13:39
  • 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. – Kev Jan 24 '12 at 13:45
  • @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. – Darin Dimitrov Jan 24 '12 at 13:59
  • Ah, sorry Darin, stupid me didn't read your update properly and missed the the word *multiple* :/. Got it now. – Kev Jan 25 '12 at 13:20
  • 2
    It's not true that you can have only one System.Web.PreApplicationStartMethodAttribute per application... only one *per assembly*. – Jeff Putz Jul 31 '12 at 20:56
  • 1
    @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. – MartinF May 27 '13 at 17:48
  • 2
    It's absolutely true. And I believe as of .Net 4.5, it's not even restricted to one per assembly anymore. – Jeff Putz May 30 '13 at 20:19
  • 1
    In 4.5 you can have multiple PreApplicationStartMethodAttribute within the same assembly. http://stackoverflow.com/questions/11800954/why-multiple-using-of-preapplicationstartmethodattribute-isnt-complied/11800997#11800997 – Eivind T Jul 26 '13 at 07:32