mafu
mafu

Reputation: 32650

How can I compose a WCF contract out of multiple interfaces?

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.

Upvotes: 2

Views: 1415

Answers (3)

Dan Meineck
Dan Meineck

Reputation: 21

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.

Upvotes: 0

marc_s
marc_s

Reputation: 754518

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.

Upvotes: 1

mafu
mafu

Reputation: 32650

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.

Upvotes: 1

Related Questions