tags:

views:

109

answers:

3

I've got multiple interfaces. All of them should be inherited and exposed by a single contract interface.

interface A { void X(); }
interface B { void Y(); }

interface C: A, B {} // this is the public contract

How is this possible? I can't add ServiceContract to A and B because that would lead to multiple endpoints. And I don't want to new-override every method in C.

+1  A: 

I think I figured it out while writing the question. Since this may help anyone with the same question I'm dropping it here...

Simply add [OperationContract] to every exposed method in the base interfaces (both A and B) as well as [ServiceContract] to the composed interface C?

I can't verify this answer right now so I still welcome any feedback :)

An InvalidOperationException is raised if ServiceContract is not applied to the sub-interfaces as well ([...] OperationContractAttribute can only be used on methods in ServiceContractAttribute types or on their CallbackContract types.), so I guess this is required.

mafutrct
I have done that in a couple of situations and it works.
veggerby
+1  A: 

You're are absolutely correct that attributes like [ServiceContract] and such are not inherited - you need to explicitly set them on any interface that should be a service contract. The same applies to [DataContract] attributes on concrete data classes - those are not inherited, either - if a descendant class of a data contract should itself be a data contract, it needs to be explicitly marked up as much. WCF by default tries to make you be very explicit about your intent (which is a good thing, I would say).

Not sure what you gain from composing interfaces like this, but you can most definitely have a service implementation (concrete class) which implements multiple valid WCF service contracts (interfaces) - no problem at all with that setup.

marc_s
Just to clarify: I'm talking about a single contract. This contract is composed of multiple interfaces.
mafutrct
A: 

Perhaps this will help? http://www.meineck.net/2008/04/wcf-hosting-multiple-wcf-services-as.html

I'm not sure if it's what you want but it explains how to host multiple services on a single endpoint by using partial classes.

Dan Meineck
Oh, I'm only talking about the contract itself here, not the implementation. Your answer applies to a different question of mine though: http://stackoverflow.com/questions/2446310/how-can-i-avoid-huge-communication-classes-in-wcf
mafutrct