Search code examples
c#wcferror-handlingduplex

How do I throw notify a client of a fault on a callback channel?


I have a duplex channel that uses callbacks to provide updates as part of a subscription to some data (see below for interfaces). Occasionally that data is unavailable and when it is the server needs to shut down its subscription and let the client open a new one, so I want to notify the client that it needs to do so.

  • What is the correct way to do so?
  • Is it possible to pass back a FaultException via the IUpdate interface? It seems that the throw new FaultException(...); syntax is specific to request-response.
  • Do I need return a custom fault somehow?
  • Do I just have to add a new OnError function or similar that explicitly forces the client to reconnect?

.

[ServiceContract(Namespace = "http://tempura.org/ISubscribe", Name = "ISubscribe", ConfigurationName = "Subscribe", CallbackContract = typeof(IUpdate))]
interface ISubscribe
{
    [OperationContract]
    long Subscribe();

    [OperationContract(IsOneWay = true)]
    void Unsubscribe(long index);
}

[ServiceContract(Namespace = "http://tempura.org/ISubscribe")]
public interface IUpdate
{
    [OperationContract(IsOneWay = true)]
    void OnUpdate(string update);
}

Solution

  • Using what you have there, sending (string update) between the host and client, you can't throw and catch the error. From my experience, you'd either need to:

    a) add another function, as you suggest "add a new OnError function ... ", containing an error flag and string for the message, or

    b) change the String in OnUpdate function to an object containing your return string, but also a bError flag, a string for an error message or perhaps an Exception object.

    Then, as the client is looping and listening for an inbound message, check the new function or object for your error conditions.

    That's what I'm doing ...