davidebbo / WebActivator

Apache License 2.0
242 stars 49 forks source link

ApplicationShutdownMethodAttribute isn't working on .NET 4.0 #11

Open adamjasinski opened 11 years ago

adamjasinski commented 11 years ago

On .NET Framework 4.0, methods registered with ApplicationShutdownMethodAttribute are not called when a web application is shutting down. They work as expected when the host machine has .NET Framework 4.5.

Reason: WebActivator considers an application shutting down when a number of its special 'counter' modules (StartMethodCallingModule) goes down to 0. This doesn't work properly on .NET 4.0 because of a bug in HttpApplicationFactory, which doesn't dispose a 'special' pool of HttpApplications. (see http://connect.microsoft.com/VisualStudio/feedback/details/632507/httpmodules-attached-to-the-special-httpapplication-instance-are-not-disposed) As a result, some copies of StartMethodCallingModule will remain undisposed, their counter won't go down to 0, and ApplicationShutdownMethodAttribute methods won't be called. This HttpApplicationFactory bug seems to be fixed in .NET 4.5 ('special' HttpApplication pool is getting disposed).

davidebbo commented 11 years ago

Ah, I didn't know that. Do you know if there is a way to fix it so it works in both 4.0 and 4.5? I don't think there is any HttpApplication event that can help here.

adamjasinski commented 11 years ago

No, there doesn't seem to be any event that would be guaranteed to happen after ALL HttpApplications and HttpModules are disposed. Application_End event is fired BEFORE HttpApplications/HttpModules from this 'special' pool are disposed (which doesn't make it good enough for uses like DI container disposal).

davidebbo commented 11 years ago

Well, if someone comes up with a solution to this, feel free to send a pull request! Until then, I guess it's a 4.5 only feature...