Unity configuration missing after restarting application pool

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 works under IIS7, and everything works fine when it starts.

It stops working whenever the application pool is recycled. The configuration is somehow messed up and it can no longer resolve my classes. However, the static configuration field in the UnityFactory class still contains the configuration that was provided for the first time. Thus, the class itself has not changed.

The Application_Start method does not start after reusing the application pool, so the configuration is not applied again.

If I set a breakpoint and apply the configuration again, everything will work again.

What's going on here? Why does Unity forget about all my activities? And is there an event that I can subscribe to that lets me know when the pool was redesigned?

+4
source share
1 answer

Obviously, when the application pool is processed, the assemblies are freed, which leads to the fact that Unity cannot find the declared classes in it.

I managed to solve this effect by specifically downloading builds for Unity. Thus, they remain in memory even when the application pool is being processed.

 public static void Configure(Func<IAutoRegistration,IAutoRegistration> configuration) { // Create new UnityContainer with auto registration container = new UnityContainer(); var autoRegistration = container.ConfigureAutoRegistration(); // Load assemblies var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin"); foreach (string dll in Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories)) { autoRegistration.LoadAssemblyFrom(dll); } // Apply configuration configuration(autoRegistration).ApplyAutoRegistration(); } 

And to answer my questions:

What's going on here? Why does Unity forget about all my classes?

When the application pool is processed, assemblies can be freed from memory. When they are needed again, they reboot. However, since they seem to come from different files, Unity cannot know that they are in fact the same assemblies as before, and therefore cannot find the original class definitions.

And is there an event that I can subscribe to that lets me know when the pool was redesigned?

There seems to be no such thing.

+2
source

Source: https://habr.com/ru/post/1446284/


All Articles