2013-02-15 42 views
2

我有我的业务接口如下声明:不一致合同

[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"属性并不不仅界面 - >类,但也界面 - >界面关系的工作。当我将MyCustomContractBehavior加到ITestService时,它开始工作得很好。

但后来找到我了 - 嘿,但是ServiceKnownType呢?我没有将它添加到ITestService,但仍然对我的ITestService合同通过了所有集成测试!当我将ServiceKnownType评论为IMyService时,我的许多测试都失败了,所以显然ServiceKnownType在某种程度上奇迹般地从父接口“继承”。

看来,WCF会检查接口层次结构已知类型的搜索时,但对于行为搜索时它不这样做。这是真的还是我误解了一些东西?

回答

0

标有[ServiceContract]ITestService?您提供的IMyService的定义不是。在您将界面标记为合同(或继承合同)之前,您的合同行为应该没有任何影响。

+0

是的,我在IMyService和ITestService上都有[ServiceContract]。奇怪的是,我只能在IMyService上使用ServiceKnownType,它也可以“自动地”在ITestService上工作,但对于MyCustomContractBehavior属性,我必须明确地将它设置在ITestService上才能使其工作。 – JustAMartin 2013-02-18 08:54:48