views:

390

answers:

4

Hi,

I have a service exposed via WCF. The service exposes several methods that talk to the database through a Linq to SQL datacontext. The datacontext is bound to the CallContext. All of this is working as it should but I can't figure out the proper place to dispose the Linq to SQL datacontext. Please help.

A: 

I believe that best practice is to create and dispose Linq to SQL context in every call.

public void DoSomething(){

   using(var c = new MoldeContext()){
       // Do something..
   }
}
Mike Chaliy
A: 

I think I found the answer. I'll mark this as the answer if there isn't a better one by tomorrow. I used the OperationContext.Current.OperationCompleted event to dispose the DataContext.

Ali Kazmi
A: 

I've found this Unit of Work approach really helpful. The blog post explains very well the trade-offs between the options. Also, you might want to check this post that deals with threading issues.

vladhorby
A: 

In this post Stephen Walther says that we should not Dispose the DataContext http://stephenwalther.com/blog/archive/2008/08/20/asp-net-mvc-tip-34-dispose-of-your-datacontext-or-don-t.aspx

name1ess0ne