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.
Asked
Active
Viewed 1,731 times
0
-
What do you mean when say "bound to the CallContext"? – Mike Chaliy Nov 29 '09 at 12:46
-
CallContext.SetData("DataContext", new ModelContext()); – Ali Kazmi Nov 29 '09 at 13:44
4 Answers
1
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
- 358
- 4
- 6
0
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
- 25,801
- 18
- 67
- 105
0
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
- 3,610
- 6
- 35
- 51
-1
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
- 868
- 7
- 17