.net 来自单个 WCF 服务的多个接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple interfaces from a single WCF service?
提问by Peter Wone
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.
我的意思是一种服务,在一个端点上提供多个接口。
回答by blowdart
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.
然后在你的实现类中实现它们。你不能做的是将多个接口和多个实现神奇地合并到一个端点中。
回答by Joseph Simpson
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.
这是可能的,因为传入的消息会根据地址和合同过滤器的组合路由到适当的端点。
回答by cruizer
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 客户端)。
回答by sipwiz
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>
回答by vizmi
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.
如果您的实现类变得太大(就像我的一样),请尝试在部分类中实现超级接口。您可以将一个接口实现放入一个文件中。这只是一个约定,但以后可能会有用。

