Can I prevent WCF from rolling back a transaction when an error occurs?

Consider the following WCF service, which is involved in a distributed transaction. The normal behavior of WCF is to roll back a transaction if any error occurs. Is there a way to override this behavior?

Service Agreement:

[ServiceContract]
public interface ITestService {
    [OperationContract]
    [FaultContract(typeof(TestServiceFault))]
    void ThrowError();
    [OperationContract]
    void DoSomething();
    [OperationContract]
    void DoSomethingElse();
}

[DataContract]
public class TestServiceFault{}

Service implementation:

class TestService : ITestService {
    [OperationBehavior(TransactionScopeRequired = true)]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    public void ThrowError() {
        throw new FaultException<TestServiceFault>(new TestServiceFault());
    }
    [OperationBehavior(TransactionScopeRequired = true)]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    public void DoSomething() {
        //
        // ...
        //
    }
    [OperationBehavior(TransactionScopeRequired = true)]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    public void DoSomethingElse() {
        //
        // ...
        //
    }
}

Customer implementation fragment:

using(new TransactionScope()) {
    testServiceClient.DoSomething();

    try {
        testServiceClient.ThrowError();
    } catch(FaultException<TestServiceFault>) {}

    testServiceClient.DoSomethingElse();
}

When a FaultException is thrown from ThrowError () , WCF rolls back the distributed transaction, which includes the work performed by DoSomething () . Then, the DoSomethingElse () client call completes with the message. The current transaction cannot be canceled. The following exception occurred: the transaction was already implicitly or explicitly committed or aborted.

. . - , , .

. FaultException WCF ?, . - , .

+3
1

, TransactionAutoComplete = false , SetTransactionComplete(), .

+4

Source: https://habr.com/ru/post/1792854/


All Articles