我有几个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
,它似乎都没有。
答案 0 :(得分: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
}
没有对此进行测试,但我认为没有任何理由说明这不起作用。