Reputation: 2712
i have a big problem to use or understand the use of transaction with wsdualhttpbinding WCF.
i have something like this :
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 So here im in my Client an try to execute an Operation with Transaction requirements :
if (Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty)
{
using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
try
{
//do some stuff
}
}
}
Well im getting an Error : Transaction.Current is empty
Thank u for helping
EDIT : I just needed to place the if after the using
using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
if (Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty)
{
try
{
//do some stuff
}
}
}
Upvotes: 0
Views: 971
Reputation: 2697
Outside of a TransactionScope, I think Transaction.Current will always be null. You need to enter the transaction scope first, then start accessing properties of Transaction.Current. It looks like you are trying to execute some non-transactional operation at the client? If so, try this:
using (TransactionScope tran = new TransactionScope())
{
if (Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty)
{
// ambient transaction is not escalated; exclude this operation from the ambient transaction
using (TransactionScope tran2 = new TransactionScope(TransactionScopeOption.Suppress))
{
// do some stuff
}
}
else
{
// ambient transaction is escalated
// do some other stuff
}
}
Note: I've copied the condition as you have it in your example code, but you should verify that this is the right test. According to MSDN, TransactionInformation.DistributedIdentifier
is null
, not Guid.Empty
, outside of a distributed transaction.
Upvotes: 1
Reputation: 51329
I think you want to add AutoEnlistTransaction=true on your OperationBehavior attribute. Likewise, you may want to add AutoCompleteTransaction=true.
Upvotes: 0