【发布时间】:2011-07-30 21:17:30
【问题描述】:
使用 wsdualhttpbinding WCF 的事务使用或理解使用有很大的问题。
我有这样的事情:
IService:
[ServiceContract]
public interface IService
{
//...
[OperationContract]
[ApplyDataContractResolver]
[TransactionFlow(TransactionFlowOption.Mandatory)]
bool SaveDevice(Device device);
//...
}
Service.svc.cs:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service : IService
{
[OperationBehavior(TransactionScopeRequired = true)]
public bool SaveDevice(Device device)
{
bool temp = false;
Transaction transaction = Transaction.Current;
using (EntityConn context = new EntityConn())
{
try
{
//....
}
}
}
}
Model.cs 因此,我在我的客户中尝试执行具有事务要求的操作:
if (Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty)
{
using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
try
{
//do some stuff
}
}
}
我得到一个错误:Transaction.Current 是空的
- 我走对了吗?
- 如果没有,我该如何解决?
感谢您的帮助
编辑:我只需要将 if 放在 using 之后
using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
if (Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty)
{
try
{
//do some stuff
}
}
}
【问题讨论】:
-
您的服务正在实现 IService,但您发布的代码有 IVdmService....
-
谢谢蒂姆!这只是一个错误!
-
您是否在both 服务和客户端绑定中设置了 transactionFlow 属性(以编程方式或通过配置)?
-
是的!两个都!交易流=真!
标签: c# wcf transactions