In my Application_Start
I configure Unity using the Unity-AutoRegistration tool:
UnityFactory.Configure(config => config
.Include(If.ImplementsITypeName, Then.Register())
.ExcludeSystemAssemblies()
);
My UnityFactory
class is static. Configure
works as follows:
public static void Configure(Func<IAutoRegistration,IAutoRegistration> configuration)
{
// Store the configuration to be able to apply it again when needed
UnityFactory.configuration = configuration;
// Create new UnityContainer
container = new UnityContainer();
// Apply configuration
configuration(container.ConfigureAutoRegistration()).ApplyAutoRegistration();
}
It runs under IIS7 and all works fine when when it's started.
It stops working whenever the application pool has been recycled. The configuration somehow gets messed up, and it is not able to resolve my classes anymore. However, the static field configuration
in the UnityFactory
class still contains the configuration as was provided the first time. So the class itself hasn't changed.
The Application_Start
method is not triggered after the application pool has been recycled, so the configuration is not applied again.
If I set a breakpoint and manually apply the configuration again, it all works again.
What is happening here? Why is Unity forgetting about all my classes? And is there an event I can subscribe to which allows me to know when the pool has been recycled?