WCF - Handling Streaming Errors
For streaming support, I am returning a message overridden to OnWriteBody ...
The problem is that an exception is thrown in OnWriteBody (DB timeout or whatever)
ProvideFault in IErrorHandler is not being called and therefore I have no way to propagate the error to the client ( via filtering in IErrorHandler).
Is there any way to solve this problem.
Thanks.
a source to share
when doing streaming with WCF, I create two ServiceContracts that do the streaming that will send a notification at the end of the streaming.
ServiceContract's answer I'm using a duplex type binding. Client to invoke the ServiceContract response first to get a ticket for their transaction, then call my ServiceContract. Then, at the end of the transaction, the client will receive a success or failure notification from the ServiceContract response.
[ServiceContract]
public interface IStreamFileService
{
[OperationContract]
void Upload(Stream stream);
}
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ITransferCallback))]
public interface IStreamFileResponseService
{
[OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)]
Guid StartUpload();
}
[ServiceContract]
public interface ITransferCallback
{
[OperationContract]
void OperationComplete(ResponseMessage response);
}
I do this in two services because my requirements and workflow require me to track a lot of things and authenticate, verify, etc.
a source to share
OnWriteBody is called when response headers, such as 200, have already been sent to the client. Where it is impossible to handle errors at this stage. Two workarounds / tricks I've used:
- Wait for the first line of data before sending 200 (for example, before returning a message) and repeat further lines inside OnWriteBody. This will work because most SQL errors and timeouts happen before showing any data. However, do not cover cases where an error occurs when repeating results.
- Has special error handling code on the client and server. For example, in case the error server inside OnWriteBody can serialize the error and send it as a special Data-Row. The client should expect such a special Data-Row, receiving a response and processing accordingly.
a source to share