我的服务接口有以下声明:
[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不起作用。它的构造函数被调用但ApplyClientBehavior不ApplyDispatchBehavior被调用。
好的,所以我推断该"Inherited = true"属性不仅适用于 interface->class,而且适用于 interface->interface 关系。当我添加MyCustomContractBehavior到 时ITestService,它开始工作得很好。
但是后来我想到了-嘿,但是呢ServiceKnownType?我没有将它添加到合同通过ITestService的所有集成测试中,但仍然是!ITestService当我注释掉 时ServiceKnownType,IMyService我的许多测试都失败了,所以显然ServiceKnownType不知何故从父界面神奇地“继承”了。
在搜索已知类型时,WCF 似乎正在检查接口层次结构,但在搜索行为时却没有这样做。这是真的还是我误解了什么?