Linq to Sql DataContext life cycle in a WCF service
I have a service exposed via WCF. The service exposes several methods that talk to the databas开发者_JAVA百科e 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.
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.
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..
}
}
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.
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
精彩评论