单个WCF服务的多个接口?

时间:2022-09-25 12:11:03

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

单个WCF服务可以提供多个接口,如果是这样,您将如何在app.config中表达这一点?

I mean one services offering several Interfaces on one endpoint.

我的意思是一个服务在一个端点上提供多个接口。

5 个解决方案

#1


50  

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.

然后在实现类中实现它们。你不能做的是有多个接口,多个实现神奇地合并到一个端点。

#2


23  

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

单个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.:

上面链接的示例解释了可以在同一物理地址(listenUri)上注册多个端点,每个端点实现不同的接口(契约),例如:

<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.

这是可能的,因为传入消息基于地址和合同过滤器的组合路由到适当的端点。

#3


5  

With WCF, you can:

使用WCF,您可以:

  • 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).
  • 有一个服务实现类通过多个端点公开,例如一个服务端点使用BasicHttpBinding实现最大互操作性,另一个端点使用NetTcpBinding实现最高性能(使用WCF客户端)。

#4


3  

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

以下是如何在App.Config中的两个不同端点上公开相同的接口,如果您要问的话。

<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>

#5


3  

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.

如果你的实现类变得太大(比如我的),请尝试在部分类中实现超级接口。您可以将一个接口实现放入一个文件中。它只是一个惯例,但以后可能会有用。

#1


50  

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.

然后在实现类中实现它们。你不能做的是有多个接口,多个实现神奇地合并到一个端点。

#2


23  

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

单个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.:

上面链接的示例解释了可以在同一物理地址(listenUri)上注册多个端点,每个端点实现不同的接口(契约),例如:

<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.

这是可能的,因为传入消息基于地址和合同过滤器的组合路由到适当的端点。

#3


5  

With WCF, you can:

使用WCF,您可以:

  • 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).
  • 有一个服务实现类通过多个端点公开,例如一个服务端点使用BasicHttpBinding实现最大互操作性,另一个端点使用NetTcpBinding实现最高性能(使用WCF客户端)。

#4


3  

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

以下是如何在App.Config中的两个不同端点上公开相同的接口,如果您要问的话。

<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>

#5


3  

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.

如果你的实现类变得太大(比如我的),请尝试在部分类中实现超级接口。您可以将一个接口实现放入一个文件中。它只是一个惯例,但以后可能会有用。