I have several WebActivator.PreApplicationStartMethod decorated classes.
One is for Ninject, another class for AwesomeMVC and a third one is for background task scheduler.
The problem is that the scheduler class needs to take advantage of the dependecies, that are resolved by IoC container.
My questions are:
Yes, you can have as many classes as you want which have a WebActivator.PreApplicationStartMethod
assembly attribute pointing to them. Many NuGet packages use this technique to enable them to bootstrap into your application without editing Global.asax.
You can define the order too. You can pass a named parameter, Order
in the PreApplicationStartMethod
call. The WebActivator framework will ensure that the methods are called in the order specified. For example, to make your IoC framework register first, do something like this:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.NinjectWebCommon), "Start", Order=1]
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.BGScheduler), "Start", Order=2]
Because WebActivator
classes are static classes, I don't see how you can use constructor injection in them. You can however, use the service locator (anti?)-pattern by registering your IoC resolver as Mvc's default service locator, using System.Web.Mvc.DependencyResolver.SetResolver(IDependencyResolver resolver)
.
I don't particularly want to go into the benefits and drawbacks of the service locator pattern here though!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With