WCF服务合同上的属性不一致

时间:2013-02-15 15:17:38

标签: wcf inheritance interface custom-attributes

我的服务界面有以下声明:

[MyCustomContractBehavior]
[ServiceKnownType("GetKnownTypes", typeof(ServiceKnownTypesDiscoveryHelper))]
public interface IMyService

其中MyCustomContractBehavior如下:

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
public class MyCustomContractBehavior: Attribute, IContractBehavior

然后我创建了一个新的服务接口,扩展了我的第一个接口:

public interface ITestService : IMyService

在测试时,我注意到MyCustomContractBehavior不起作用。它的构造函数被调用,但ApplyClientBehaviorApplyDispatchBehavior不被调用。

好的,所以我推断"Inherited = true"属性不仅适用于interface->类,还适用于interface->接口关系。当我将MyCustomContractBehavior添加到ITestService时,它开始正常工作。

然后它来找我 - 嘿,但ServiceKnownType呢?我没有将它添加到ITestService,但我ITestService合同的所有集成测试仍然通过!当我在ServiceKnownType上注释掉IMyService时,我的许多测试都失败了,所以显然ServiceKnownType会从父界面神奇地“继承”。

WCF似乎在搜索已知类型时检查接口层次结构,但在搜索行为时却没有这样做。是真的还是我误解了什么?

1 个答案:

答案 0 :(得分:0)

ITestService是否标有[ServiceContract]?您提供的IMyService的定义不是。在您将接口标记为合同(或继承合同)之前,您的合同行为应该没有效果。

相关问题