我有我的业务接口如下声明:不一致合同
[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"
属性并不不仅界面 - >类,但也界面 - >界面关系的工作。当我将MyCustomContractBehavior
加到ITestService
时,它开始工作得很好。
但后来找到我了 - 嘿,但是ServiceKnownType
呢?我没有将它添加到ITestService
,但仍然对我的ITestService
合同通过了所有集成测试!当我将ServiceKnownType
评论为IMyService
时,我的许多测试都失败了,所以显然ServiceKnownType
在某种程度上奇迹般地从父接口“继承”。
看来,WCF会检查接口层次结构已知类型的搜索时,但对于行为搜索时它不这样做。这是真的还是我误解了一些东西?
是的,我在IMyService和ITestService上都有[ServiceContract]。奇怪的是,我只能在IMyService上使用ServiceKnownType,它也可以“自动地”在ITestService上工作,但对于MyCustomContractBehavior属性,我必须明确地将它设置在ITestService上才能使其工作。 – JustAMartin 2013-02-18 08:54:48