每个 WCF 请求使用一个 LINQ to SQL DataContext 并自动调用 SubmitChanges?
我已经在 web/SO 上搜索了好几个小时了,所以如果我错过了,请原谅我...
我正在寻找一个具体的示例,说明如何在作为 IIS 托管的 WAS 运行时挂钩到 WCF 管道,以便创建一个新的 LINQ当 WCF 请求开始和结束时,SQL DataContext 会自动创建和释放。
另外,当 DataContext 被处置时,是否可以/可以调用 SubmitChanges()
来提交在请求过程中所做的任何更改?
我的主要目标是:
- 将 DataContext 生命周期与请求相关联
- 从我的域层隐藏 DataContext,以便轻松测试。
I've been scouring the web/SO for hours now so exuse me if I missed it...
I am looking for a concrete example of how to hook into the WCF pipeline when running as a WAS hosted in IIS so that a new LINQ to SQL DataContext is automatically created and disposed when the WCF request begins and ends.
Also, when the DataContext is being disposed would it be possible/ok to call SubmitChanges()
at that point to commit any changes that have been made over the course of the request?
My main goals here are to:
- Have the DataContext lifecycle be tied to the request
- Hide the DataContext from my domain layer so that it is easily testable.
一种方法是扩展为 WCF 中调用的每个操作创建的 OperationContext 实例。您可以向OperationContext 添加扩展。此扩展可以包含可以在不同位置访问和使用的 DataContext 实例。看看这篇帖子
One way to go about it would be to extend the OperationContext instance which gets created for each operation invoked in WCF. You can add extension to OperationContext. This extension can contain the DataContext instance which can be accessed and used at different location. Look at this post