Search code examples
wcfinheritancedatacontract

FaultContract inheritance


I have several WCF service contracts, all of which contain exactly the same method StopOperation, having the same signature:

[ServiceContract]
public interface IMyServiceA
{
    [FaultContract(typeof(ServiceAError))]
    [OperationContract]
    void StopOperation(TaskInformation taskInfo);

    // other specific methods
}

What I'd like to be able to do is to extract StopOperation into an interface, IStoppable, and have all my services inherit this operation. However, I have a problem with the FaultContract definition, as it defines a concrete fault type.

Is it possible to have FaultContract refer to an abstract ErrorBase type, and have the concrete ones specified by KnownContract somehow? Kind of like:

[ServiceContract]
public interface IStoppable
{
    [FaultContract(typeof(ErrorBase))]
    [OperationContract]
    void StopOperation(TaskInformation taskInfo);
}

No matter where I tried specifying KnownContract, it didn't seem to take.


Solution

  • Have you tried using a generic type ?

    For instance:

    [ServiceContract]
    public interface IStoppable<T> where T : ErrorBase
    {
        [FaultContract(typeof(T))]
        [OperationContract]
        void StopOperation(TaskInformation taskInfo);
    }
    

    Then you'd say

    [ServiceContract]
    public interface IMyServiceA : IStoppable<ServiceAError>
    {
        // other specific methods
    }
    

    Haven't tested this, but I don't see any reason why this shouldn't work.