4

我有几个 WCF 服务合同,所有这些合同都包含完全相同的方法StopOperation,具有相同的签名:

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

    // other specific methods
}

我希望能够做的是提取StopOperation到一个接口中IStoppable,并让我的所有服务都继承这个操作。但是,我对FaultContract定义有疑问,因为它定义了具体的故障类型。

是否FaultContract可以引用抽象ErrorBase类型,并以某种方式指定具体类型KnownContract?有一些像:

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

无论我在哪里尝试指定KnownContract,它似乎都没有。

4

1 回答 1

2

您是否尝试过使用泛型类型?

例如:

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

那你会说

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

尚未对此进行测试,但我看不出有任何理由为什么这不起作用。

于 2012-12-13T16:39:35.990 回答