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?
Pedro and Darrel both gave great suggestions. In the solution I finally came up with, I ended up using a message handler:
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.