Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple interfaces from a single WCF service?

Can a single WCF service offer multiple interfaces, and if so how would you express this in app.config?

I mean one services offering several Interfaces on one endpoint.

like image 928
Peter Wone Avatar asked Apr 06 '09 06:04

Peter Wone


People also ask

Can we have multiple endpoints in WCF?

WCF allow us to give multiple base addresses for each type of protocol. And at the run time corresponding endpoint will take the base address. So you can expose IService1 on multiple EndPoint with more than one binding as below.

Can you use two interfaces at once in C#?

C# allows the implementation of multiple interfaces with the same method name.

Can a service have multiple endpoints?

As demonstrated in the Multiple Endpoints sample, a service can host multiple endpoints, each with different addresses and possibly also different bindings. This sample shows that it is possible to host multiple endpoints at the same address.


2 Answers

First you need to be clear what a service is. Do you mean a single endpoint, or multiple endpoints in the same host?

Assuming you mean a single endpoint, then yes, but with a little work. An endpoint can only implement a single interface; so what you need to do is combine all the interfaces you want to implement into a single interface

public interface IMyInterface : IInterface1, IInterface2 

and then implement them all inside your implementation class. What you cannot do is have multiple interfaces and multiple implementations magically merge into a single endpoint.

like image 200
blowdart Avatar answered Sep 20 '22 13:09

blowdart


The following looks closer to the original goal and doesn't involve one large interface...

Multiple Endpoints at a Single ListenUri: http://msdn.microsoft.com/en-us/library/aa395210.aspx


The sample linked to above explains that it's possible to have multiple endpoints registered at the same physical address (listenUri), each implementing a different interface (contract), e.g.:

<endpoint address="urn:Stuff"         binding="wsHttpBinding"         contract="Microsoft.ServiceModel.Samples.ICalculator"          listenUri="http://localhost/servicemodelsamples/service.svc" /> <endpoint address="urn:Stuff"         binding="wsHttpBinding"         contract="Microsoft.ServiceModel.Samples.IEcho"          listenUri="http://localhost/servicemodelsamples/service.svc" /> <endpoint address="urn:OtherEcho"         binding="wsHttpBinding"         contract="Microsoft.ServiceModel.Samples.IEcho"          listenUri="http://localhost/servicemodelsamples/service.svc" /> 

This is possible because incoming messages are routed to the appropriate endpoint based on a combination of address and contract filters.

like image 22
Joseph Simpson Avatar answered Sep 16 '22 13:09

Joseph Simpson