6

I am referencing a 3rd party library in an ASP.NET MVC application, because the design of the library, it requires a class (say ClassA) from it must be instantiated only once and should be disposed after use. So, in my MVC application I defined a static class and instantiated the external ClassA inside (ClassA is heavily used). But what's the effective way to dispose the single instance of ClassA? I believe Application_End is not the right place. but in what event or method is the right place to do it?

I should consider scenarios iisreset, application pool recycle and any other cases that server stops or restarts and to make sure the resource is properly disposed.

Shuping
  • 5,388
  • 6
  • 43
  • 66
  • What is the resource that needs disposal? If the process ends, are you sure you need to do anything else? How would you cope with (say) a power cut? – Jon Skeet Jul 10 '15 at 08:09
  • Kind of confused. Can you use the Using keyword so that the object is automatically disposed of correctly? Not sure why you want to do a iisreset everytime the object is used. – SoftwareCarpenter Jul 10 '15 at 08:10
  • @JonSkeet because the external library create native resources, I need dispose it. – Shuping Jul 10 '15 at 08:19
  • 1
    Are you *sure*? Are these native resources things like network connections, Win32 objects etc which will be killed when the process terminates anyway? Again - what would go happen if there was a power cut, so you had no chance to dispose of those resources? – Jon Skeet Jul 10 '15 at 08:20
  • @SoftwareCarpenter, the class can only be instantiated once (due to the underline native code issue), and you know there are concurrent HTTP requests, so I should not use "using" clause to create and dispose the instances. but I create an "globally" object and use all cross all requests and want to dispose it in some proper place. – Shuping Jul 10 '15 at 08:22
  • You cannot ensure that disposal will happen on app shutdown. Worker processes may crash. Your design must make due without guaranteed shutdown. – usr Jul 10 '15 at 08:23
  • 1
    @JonSkeet, agree I cannot handle all cases (e.g. power cut), but I would like to try best to handle all cases that can be handled. And I hope all the native resources will be auto killed by OS. However, in the case such as IIS application pool recycle, the process is not terminated, but all requests and responses are on hold, not sure this is the problem is the resource is not disposed. – Shuping Jul 10 '15 at 08:26
  • Can you not make your extended class static and instead use a static property of ClassA? This way your extended class will dispose of the object properly. @JonSkeet is expert so I am sure he will be able to help. – SoftwareCarpenter Jul 10 '15 at 08:28
  • 1
    If the AppDomain goes away, all the objects within it can be garbage collected. If the library has been written properly (using SafeHandle or finalizers etc) it *should* all just be collected appropriately. You *could* use AppDomain.DomainUnload, but it doesn't sound ideal. Perhaps you should ask the third party producing the library? Basically, "singleton which needs disposal" is a really, really ugly pattern. – Jon Skeet Jul 10 '15 at 08:28
  • Thanks all your quick comments and answers, I have a better understanding now. – Shuping Jul 10 '15 at 08:43
  • Doesn't a dependency injection container cover this scenario? Sounds like you're basically trying to manually handle what a DI container does out of the box. – Chris Pratt Jul 10 '15 at 16:01
  • 1
    You can Try something like this: http://stackoverflow.com/a/13258842/2649492 – MAD Jul 15 '15 at 17:26

1 Answers1

0

My suggestion. First create a private field for ClassA. Encapsulates it to a property that checks whether or not the field is null. If is null create a new instance. If not, dispose the object and return a new instance of ClassA. Does it makes sense?

Bruno
  • 71
  • 4