Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guidelines For Dispose() and Ninject

So, I have a method exposed from a WCF service as such:

public GetAllCommentsResponse GetAllComments(GetAllCommentsRequest request) {     var response = new GetAllCommentsResponse();      using(_unitOfWork)         try         {             Guard.ArgNotNull(request, "request");              var results = _unitOfWork.CommentRepository.Get(d => d.Id > 0).ToArray();              //... Do rest of stuff here         }         catch (Exception ex)         {             response.Success = false;             response.FailureInformation = ex.Message;             Logger.LogError("GetAllComments Method Failed", ex);         }      return response; } 

I have a global DataUnitOfWork object (which implements IDisposable) that gets instantiated by Ninject through a constructor argument when a service call comes in. When debugging, if I use

using(_unitOfWork) 

the _unitOfWork object gets disposed immediately after going out of scope then gets called again by Ninject (although it's been marked as disposed, so nothing happens.) Without the using statement, Ninject handles the disposing.

Long story short, is there a general rule of thumb for this? I've been scared of the whole IDisposable thing after everything I read seems to indicate never to use it, or use it in certain eclectic situations, but it's always confused me.

Any input is appreciated.

Oh, also while I'm here typing anyway, why exactly is there a call to GC.SuppressFinalize() when disposing? How do Dispose and Finalize differ?

like image 847
Nate222 Avatar asked Apr 19 '12 18:04

Nate222


People also ask

Does ninject call Dispose?

The CLR documentation states that whoever creates a Disposable object is responsible for calling Dispose. In this case the object is created by Ninject. That means you should not call Dispose explicitly.

Why do we have the Dispose () method?

The dispose pattern is used for objects that implement the IDisposable interface, and is common when interacting with file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. This is because the garbage collector is unable to reclaim unmanaged objects.

What does dispose () do in C#?

In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.

Is Dispose method called automatically?

Dispose() will not be called automatically. If there is a finalizer it will be called automatically. Implementing IDisposable provides a way for users of your class to release resources early, instead of waiting for the garbage collector.


1 Answers

The CLR documentation states that whoever creates a Disposable object is responsible for calling Dispose. In this case the object is created by Ninject. That means you should not call Dispose explicitly.

Ninject disposes every Disposable object that has another scope other than InTransientScope as soon as the scope object to which the created object is tied is collected by GC. That's why every Disposable object should be Bindd with a scope that is not InTransientScope(). E.g. you can use InParentScope() from the NamedScope extension which will Dispose the object as soon as the object it is injected into is garbage collected.

like image 51
Remo Gloor Avatar answered Oct 08 '22 18:10

Remo Gloor