我有我的業務接口如下聲明:不一致合同
[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