Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:58:27+00:00 2026-06-09T13:58:27+00:00

I’ve got a WCF operation conceptually like this: [OperationBehavior(TransactionScopeRequired = true)] public void Foo()

  • 0

I’ve got a WCF operation conceptually like this:

    [OperationBehavior(TransactionScopeRequired = true)]
    public void Foo() 
    {
        try { DAL.Foo(); return Receipt.CreateSuccessReceipt(); }
        catch (Exception ex) { return Receipt.CreateErrorReceipt(ex); }
    }

If something goes wrong (say, foreign key constraint violaion) in executing the DAL code, control passes to the catch block as I’d expect. But when the method returns, it seems the transaction scope has sniffed out that the transaction failed, and it decides it better throw an exception to make sure to notify the caller about it.

In turn my client application does not get the receipt I want to return, but rather an exception:

System.ServiceModel.FaultException:  
  The transaction under which this method call was executing was asynchronously aborted.

What is wrong with my design?

I could have the service not catch anything, but this has it’s own problems as the service needs to use exception shielding and the client (a batch tool internal to the system) needs to log the error information. The service logs errors too, but not in the same way and to the same place as the batch.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:58:29+00:00Added an answer on June 9, 2026 at 1:58 pm

    Be careful here! If you set TransactionAutoComplete=true then if the service returns normally the transaction will be committed. Only if there is an unhandled exception (which for the most part you don’t have because you are catching exceptions and returning a receipt message) will the transaction be rolled back. See http://msdn.microsoft.com/en-us/library/system.servicemodel.operationbehaviorattribute.transactionautocomplete.aspx.

    Think about a scenario where you successfully executed some DAL calls but some other exception (e.g. NullReferenceException) occurs. Now the transaction will be committed when the method completes because no unhandled exception has occurred but the client receives an ErrorReceipt.

    For your scenario, I think you will have to manage the transactions yourself. For example:

    [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = false)]
    public Receipt Foo() 
    {   
        // Create TransactionScope using the ambient transaction
        using (var scope = new TransactionScope() )
        {
            try { DAL.Foo(); return Receipt.CreateSuccessReceipt(); scope.Complete(); }
            catch (Exception ex) { return Receipt.CreateErrorReceipt(ex); }
        }
    }
    

    You could eliminate boilerplate code by creating a helper method that wraps it all within the transaction or you could use policy injection/interception/aspects to manage transactions.

    [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = false)]
    public Receipt Foo() 
    {   
        return ProcessWithTransaction(() =>
            {
                DAL.Foo();
                return Receipt.CreateSuccessReceipt();
            }
            , (ex) =>
            {
                return Receipt.CreateErrorReceipt(ex);
            }
        );
    }
    
    T ProcessWithTransaction<T>(Func<T> processor, Func<Exception, T> exceptionHandler)
    {
        using (var scope = new TransactionScope())
        {
            try
            {
                T returnValue = processor();
                scope.Complete();
                return returnValue;
            }
            catch (Exception e)
            {
                return exceptionHandler(e);
            }
        }
    }
    

    You mention that you need to use exception shielding. If you are not averse to throwing faults when an error occurs then you could use Enterprise Library Exception Handling Block’s exception shielding which also lets you log the information on the way out (if you desire).

    If you decided to go that route your code would look something like this:

    [OperationBehavior(TransactionScopeRequired = true)]
    public void Foo() 
    {
        // Resolve the default ExceptionManager object from the container.
        ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
    
        exManager.Process(() => 
          {
              DAL.Foo(); 
              return Receipt.CreateSuccessReceipt(); 
          }, 
          "ExceptionShielding");
    }
    

    Enterprise Library (via configuration) would then catch any exceptions and replace them with a new FaultException that is returned to the client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.