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 7072221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:46:56+00:00 2026-05-28T05:46:56+00:00

One of the challenges I’m facing with the new WCF WebApi is that I

  • 0

One of the challenges I’m facing with the new WCF WebApi is that I cannot cleanly implement a UnitOfWork pattern.

As a quick background, the pattern works by starting a unit-of-work at the beginning of the request, doing some work, and then afterwards either rolling back or committing the unit of work.

It’s pretty easy to setup code which will “start” the unit-of-work, utilizing the HttpMessageHandler functionality. And via Task<> library, I can write a continuation which executes after the request is handled. However, I cannot always determine if a fault occurred so that I can rollback.

WCF has low-level support for faults, and in a traditional WCF service endpoint you can check to see if the channel has faulted (for instance, from inside an IMessageInspector). But the WebApi seems to be preventing this behavior.

The WebApi does expose an HttpErrorHandler contract. However this is very limited since you don’t have access to the instance context or service instance, so I don’t have access to my unit-of-work.

I wonder what other approaches have been used to implement the unit-of-work pattern here?

  • 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-05-28T05:46:56+00:00Added an answer on May 28, 2026 at 5:46 am

    Pedro and Darrel both gave great suggestions. In the solution I finally came up with, I ended up using a message handler:

    public class UnitOfWorkHandler : DelegatingHandler 
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
        {
            var unitOfWork = new UnitOfWork();
            unitOfWork.Begin();
            return base.SendAsync(request, cancellationToken).ContinueWith(result => 
                {
                    if (result.Result is HttpResponseMessage && ((HttpResponseMessage)result.Result).IsSuccessStatusCode) 
                    {
                        unitOfWork.Commit();
                    } 
                    else 
                    {
                        unitOfWork.Rollback();
                    }
                    return result.Result;
                });
        }
    }
    

    I would have used Darrel’s suggestion of storing a reference to the UnitOfWork on the HttpRequestMessage properties collection, however since the Task continuation is implemented as a closure I can simply reference the unit of work I created in its outer scope.

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

Sidebar

Related Questions

I'm building a program that communicates with Emacs, and one of the challenges I'm
One of the biggest challenges that I've encountered with SharePoint is that it doesn't
I'm trying to convert an ASP/VBScript OAuth library to VBA. One of the challenges
I am teaching myself C# and one of the current chapters challenges asks me
One of the neat characteristics of UTF-8 is that if you compare two strings
We are designing a web site and have run into some UI challenges that
I'm struggling with two formatting challenges in Sweave. One is the italicization of text
I'm trying to figure out how to solve some challenges that my team is
One of the challenges of being on a small team (sometimes even large organizations)
I created an AlertDialog to show the user next level challenges when one is

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.