Multiple interfaces from a single WCF service?

前端 未结 5 2064
情书的邮戳
情书的邮戳 2020-12-05 02:44

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

相关标签:
5条回答
  • 2020-12-05 02:53

    If your implementation class getting too big (like mine) try implement the super-interface in a partial class. You can put one interface implementation into one file. It's merely a convention but could be useful later.

    0 讨论(0)
  • 2020-12-05 03:03

    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.

    0 讨论(0)
  • 2020-12-05 03:08

    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.

    0 讨论(0)
  • 2020-12-05 03:08

    With WCF, you can:

    • have one service implementation class that implements multiple service interfaces
    • have one service implementation class exposed through multiple endpoints, e.g. one service endpoint using BasicHttpBinding for maximum interoperability and another endpoint using NetTcpBinding for maximum performance (with WCF clients).
    0 讨论(0)
  • 2020-12-05 03:09

    Here's how you could expose the same interface on two different endpoints in your App.Config if that's what you are asking.

    <service name="Service1">    
       <endpoint address="http://localhost:8001/service1.asmx" binding="basicHttpBinding" contract="IService" />
    </service>
    <service name="Service2">    
      <endpoint address="http://localhost:8002/service2.asmx" binding="basicHttpBinding"  contract="IService" />
    </service>
    
    0 讨论(0)
提交回复
热议问题