简体   繁体   中英

Inconsistency of attributes on WCF service contracts

I have the following declaration for my service interface:

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

where MyCustomContractBehavior is as follows:

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

And then I created a new service interface which extends my first interface:

public interface ITestService : IMyService

While testing, I noticed that MyCustomContractBehavior does not work. Its constructor gets called but ApplyClientBehavior and ApplyDispatchBehavior don't get called.

Ok, so I deduced that "Inherited = true" property does not work not only for interface->class but also interface->interface relations. When I added MyCustomContractBehavior to ITestService , it started working just fine.

But then it came to me - hey, but what about ServiceKnownType ? I did not add it to ITestService but still all of my integration tests for ITestService contract pass! When I comment out ServiceKnownType on IMyService , many of my tests fail, so obviously ServiceKnownType somehow gets magically "inherited" from the parent interface.

It seems that WCF is inspecting interface hierarchies when searching for known types, but it is not doing the same when searching for behaviors. Is it true or maybe I am misunderstanding something?

Is ITestService marked with [ServiceContract] ? The definition of IMyService that you gave is not. Until you mark your interface as a contract (or inherit from one that is), your Contract Behavior should have no effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM