WCF服务可以有多少ServiceContracts?

时间:2021-04-10 00:00:42

How many ServiceContracts can a WCF service have?

WCF服务可以有多少ServiceContracts?

Specifically, since a ServiceContract is an attribute to an interface, how many interfaces can I code into one WCF web service? Is it a one-to-one?

具体来说,由于ServiceContract是接口的属性,我可以在一个WCF Web服务中编码多少个接口?这是一对一的吗?

Does it make sense to separate the contracts across multiple web services?

跨多个Web服务分离合同是否有意义?

4 个解决方案

#1


1  

You can have a service implement all the service contracts you want. I mean, I don't know if there is a limit, but I don't think there is.

您可以拥有一个服务实现您想要的所有服务合同。我的意思是,我不知道是否有限制,但我不认为有。

That's a neat way to separate operations that will be implemented by the same service in several conceptually different service contract interfaces.

这是将几个概念上不同的服务合同接口中的相同服务实现的操作分开的简洁方法。

#2


18  

WCF services can have multiple endpoints, each of which can implement a different service contract.

WCF服务可以有多个端点,每个端点都可以实现不同的服务合同。

For example, you could have a service declared as follows:

例如,您可以按如下方式声明服务:

[ServiceBehavior(Namespace = "DemoService")]
public class DemoService : IDemoService, IDoNothingService

Which would have configuration along these lines:

哪条配置如下:

<service name="DemoService" behaviorConfiguration="Debugging">
  <host>
    <baseAddresses>
      <add baseAddress = "http://localhost/DemoService.svc" />
    </baseAddresses>
  </host>
  <endpoint 
    address =""
    binding="customBinding"
    bindingConfiguration="InsecureCustom"
    bindingNamespace="http://schemas.com/Demo" contract="IDemoService"/>
  <endpoint 
    address =""
    binding="customBinding"
    bindingConfiguration="InsecureCustom"
    bindingNamespace="http://schemas.com/Demo" contract="IDoNothingService"/>
</service>      

Hope that helps, but if you were after the theoretical maximum interfaces you can have for a service I suspect it's some crazily large multiple of 2.

希望有所帮助,但是如果你追求理论上的最大接口,你可以拥有一项服务,我怀疑它是2的疯狂大倍数。

#3


1  

@jdiaz

@jdiaz

Of course you should strive to have very different business matters in different services, but consider the case in which you want that, for example, all your services implement a GetVersion() operation. You could have a service contract just for that operation and have every service implement it, instead of adding the GetVersion() operation to the contract of all your services.

当然,您应该努力在不同的服务中具有非常不同的业务问题,但考虑您希望的情况,例如,您的所有服务都实现GetVersion()操作。您可以只为该操作获得服务合同,并让每个服务实现它,而不是将GetVersion()操作添加到您的所有服务的合同中。

#4


0  

A service can theoretically have any number of Endpoints, and each Endpoint is bound to a particular contract, or interface, so it is possible for a single conceptual (and configured) service to host multiple interfaces via multiple endpoints or alternatively for several endpoints to host the same interface.

理论上,服务可以具有任意数量的端点,并且每个端点都绑定到特定的合同或接口,因此单个概念(和配置)服务可以通过多个端点托管多个接口,或者可以为多个端点托管相同的界面。

If you are using the ServiceHost class to host your service, though, instead of IIS, you can only associate a single interface per ServiceHost. I'm not sure why this is the case, but it is.

但是,如果您使用ServiceHost类来托管服务,则只能为每个ServiceHost关联单个接口,而不是IIS。我不确定为什么会这样,但事实确实如此。

#1


1  

You can have a service implement all the service contracts you want. I mean, I don't know if there is a limit, but I don't think there is.

您可以拥有一个服务实现您想要的所有服务合同。我的意思是,我不知道是否有限制,但我不认为有。

That's a neat way to separate operations that will be implemented by the same service in several conceptually different service contract interfaces.

这是将几个概念上不同的服务合同接口中的相同服务实现的操作分开的简洁方法。

#2


18  

WCF services can have multiple endpoints, each of which can implement a different service contract.

WCF服务可以有多个端点,每个端点都可以实现不同的服务合同。

For example, you could have a service declared as follows:

例如,您可以按如下方式声明服务:

[ServiceBehavior(Namespace = "DemoService")]
public class DemoService : IDemoService, IDoNothingService

Which would have configuration along these lines:

哪条配置如下:

<service name="DemoService" behaviorConfiguration="Debugging">
  <host>
    <baseAddresses>
      <add baseAddress = "http://localhost/DemoService.svc" />
    </baseAddresses>
  </host>
  <endpoint 
    address =""
    binding="customBinding"
    bindingConfiguration="InsecureCustom"
    bindingNamespace="http://schemas.com/Demo" contract="IDemoService"/>
  <endpoint 
    address =""
    binding="customBinding"
    bindingConfiguration="InsecureCustom"
    bindingNamespace="http://schemas.com/Demo" contract="IDoNothingService"/>
</service>      

Hope that helps, but if you were after the theoretical maximum interfaces you can have for a service I suspect it's some crazily large multiple of 2.

希望有所帮助,但是如果你追求理论上的最大接口,你可以拥有一项服务,我怀疑它是2的疯狂大倍数。

#3


1  

@jdiaz

@jdiaz

Of course you should strive to have very different business matters in different services, but consider the case in which you want that, for example, all your services implement a GetVersion() operation. You could have a service contract just for that operation and have every service implement it, instead of adding the GetVersion() operation to the contract of all your services.

当然,您应该努力在不同的服务中具有非常不同的业务问题,但考虑您希望的情况,例如,您的所有服务都实现GetVersion()操作。您可以只为该操作获得服务合同,并让每个服务实现它,而不是将GetVersion()操作添加到您的所有服务的合同中。

#4


0  

A service can theoretically have any number of Endpoints, and each Endpoint is bound to a particular contract, or interface, so it is possible for a single conceptual (and configured) service to host multiple interfaces via multiple endpoints or alternatively for several endpoints to host the same interface.

理论上,服务可以具有任意数量的端点,并且每个端点都绑定到特定的合同或接口,因此单个概念(和配置)服务可以通过多个端点托管多个接口,或者可以为多个端点托管相同的界面。

If you are using the ServiceHost class to host your service, though, instead of IIS, you can only associate a single interface per ServiceHost. I'm not sure why this is the case, but it is.

但是,如果您使用ServiceHost类来托管服务,则只能为每个ServiceHost关联单个接口,而不是IIS。我不确定为什么会这样,但事实确实如此。