views:

84

answers:

2

Let's say I have this in an implementation of IInstanceProvider:

public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
    try
    {
        unitOfWork.Commit();
    }
    catch (Exception)
    {
        unitOfWork.Rollback();
        throw;
    }
    finally
    {
        unitOfWork.Dispose();
    }
}

That throw; will never be returned to the client because it is being called after the service has done it's work and returned data to the client, and is therefore done. How else can I return the exception? Or is there a better place to do this?

A: 

I think you are looking in the wrong place to do this. I think a better choice would be to implement the IDispatchMessageInspector interface and attach it to the collection exposed by the MessageInspectors property on the DispatchRuntime (through a behavior, most likely).

With this, you can inspect messages coming in and going out, and modify them if need be (which is how your exception would be realized, as a fault in the return message). Because of this, you will not just let the exception bubble up, but rather, you would change it to a fault message and set the return message to that.

casperOne
Thanks. Works like a charm.
Ryan Montgomery
A: 

I'm not as familiar with transactions in WCF as I should be. What in the above code returns the results to the client? Is it the rollback?

John Saunders