0

Let's assume i have the following ASP.NET webapi controller:

[RoutePrefix("api/Foo")]
public sealed class FooController : ApiController
{
    private static readonly NativeFooService _nativeFooService = new NativeFooService();

    // Controller actions
}

NativeFooService implements IDisposable and contains some heavy initialization code in constructor, so I really-really do not want to make it non-static member of my controller, but at the same time I want to call make sure it is disposed correctly, cause it is wrapping some native components.

The question is, should I care about calling Dispose on NativeFooService by using something like IRegisteredObject, or should I just leave all trouble to ASP.Net runtime?

Cœur
  • 37,241
  • 25
  • 195
  • 267
RX_DID_RX
  • 4,113
  • 4
  • 17
  • 27
  • 6
    This makes no sense. Either it is static or it is not. How can it be static - kept alive, thread safe - and then require regular disposing? Why not keep it around as long as needed. – TomTom Jan 20 '16 at 18:43
  • [The answer given here](http://stackoverflow.com/questions/12126598/how-and-when-are-c-sharp-static-members-disposed) may help. – Chase Jan 20 '16 at 18:46
  • I agree with @TomTom . I do not know how the NativeFooService initialization is implemented. I would think of possibilites to make this task lighter and faster. if it's not possible to do that then maybe try handle the instantiation and dispose in the ASP.NET AppPool Recycle? That way a user don't experience the long load time the first time the AppPool gets a request after a recycle. – J.Olsson Aug 11 '17 at 12:36
  • I suppose the finalizer on `NativeFooService` is already implemented and is working lightning-fast to escape any killing timeouts? Related topic: https://stackoverflow.com/questions/256077/static-finalizer – grek40 Aug 11 '17 at 14:28

0 Answers0