简体   繁体   English

WCF客户端/服务器模型

[英]WCF client/server model

I'm looking to create a rich-featured application using a client/server model, using WCF as a possible communications framework. 我希望使用WCF作为可能的通信框架,使用客户端/服务器模型创建功能丰富的应用程序。

I will host the service(s) in a Windows Service (so with WCF I'd using netTcpBinding). 我将在Windows服务中托管服务(因此使用WCF,我将使用netTcpBinding)。

Now, I suppose I could define every service operation into a single service contract, but there could be quite a few operations and I'd rather divide them into several cohesive service contracts. 现在,我想我可以将每个服务操作都定义为一个服务合同,但是可能会有很多操作,我宁愿将它们划分为几个内聚的服务合同。

Would splitting my operations into a set of Service Contracts mean that I would have to host each service on a separate port? 将我的操作分成一组服务合同是否意味着我将不得不在单独的端口上托管每个服务? (That wouldn't be ideal as I'd prefer to run it on one port.) (这不是理想的选择,因为我希望在一个端口上运行它。)

Any design tips regarding this scenario? 有关此方案的任何设计技巧? Thanks :) 谢谢 :)

Update 更新资料

Sorry for any confusion - It wasn't my intention to host each every service operation in a different contract, only to group them sensibly. 很抱歉造成任何混乱-我不是打算将每个服务操作托管在不同的合同中,而只是将它们合理地分组。 My problem stems from the fact that I thought that it wasn't possible to host multiple ServiceHosts on one port. 我的问题源于一个事实,即我认为不可能在一个端口上托管多个ServiceHosts。

<slaps forehead> <巴掌额头>

Joe says that it is possible to simply host multiple service hosts on one port. 乔说,可以在一个端口上简单地托管多个服务主机。 I think I must have misunderstood an exception message when I was trying this out originally. 我想我最初尝试此操作时一定会误解了异常消息。 If I can get this to work then I think it will be my favoured solution. 如果我可以解决这个问题,那么我认为这将是我最喜欢的解决方案。

I think that @Koystya and co's approach of implementing each interface on a single concrete object and hosting it in a single ServiceHost is also a good pragmatic solution to my question. 我认为@Koystya和co在单个具体对象上实现每个接口并将其托管在单个ServiceHost中的方法也是解决我的问题的一个实用的解决方案。 Especially if you treat that single concrete object as a sort of Facade. 特别是如果您将单个混凝土对象视为一种立面。 One downside I can think of tho, is that you wouldn't be able to have different ServiceBehaviors depending on the contract. 我可以想到的一个缺点是,您将无法根据合同拥有不同的ServiceBehaviors。

Also, I do agree with Joe's logic of when it's appropriate to implement multiple service contracts on one concrete class. 此外,我确实同意Joe的逻辑,即何时在一个具体的类上实现多个服务合同。

A service can expose multiple Service Contracts. 一个服务可以公开多个服务合同。

I would create WCF service implemented as one class, which itself implements several interfaces with [ServiceContract] attributes. 我将创建作为一个类实现的WCF服务,该类本身使用[ServiceContract]属性实现多个接口。

I suppose I could define every service operation into a single service contract, but there could be quite a few operations and I'd rather divide them into several cohesive service contracts. 我想我可以将每个服务操作都定义为一个服务合同,但是可能会有很多操作,我宁愿将它们划分为几个内聚的服务合同。

It makes sense to group operations into cohesive service contracts. 将操作分组为有凝聚力的服务合同是有意义的。 The granularity (eg number of operations per service contract) will depend on your application, but one service contract per operation seems extreme. 粒度(例如,每个服务合同的操作数)将取决于您的应用程序,但是每个操作一份服务合同似乎是极端的。

Would splitting my operations into a set of Service Contracts mean that I would have to host each service on a separate port 将我的操作分成一组服务合同意味着我将不得不在单独的端口上托管每个服务

Not at all, you can have several service contracts on the same port. 完全没有,您可以在同一端口上拥有多个服务合同。

You could have a concrete class that implements several service contracts as suggested by Koistya Navin .NET . 您可以有一个具体的类,该类可以实现Koistya Navin .NET建议的几个服务合同。 But: 但:

  • This is an internal implementation detail. 这是内部实现细节。

  • I would only consider doing this if the operation contract implementations were so closely related that it makes sense to implement them in a single class. 如果操作合同的实现紧密相关,以至于可以在一个类中实现它们,那么我只会考虑这样做。

  • In which case it may make sense to make them part of the same service contract. 在这种情况下,使它们成为同一服务合同的一部分可能是有意义的。

Why are you considering putting each operation into its own service contract? 您为什么要考虑将每项操作纳入自己的服务合同中? What do you intend to gain from that? 您打算从中获得什么?

Basically, a service contract can have as many operations marked [OperationContract] as you wish. 基本上,服务合同可以根据需要包含任意多个标记为[OperationContract]的操作。 That would probably make more sense. 那可能更有意义。

If you have disparate functions, you can always define more than one ServiceContract (as an interface), and have your service class implement them all and host the one service class (with all the ServiceContract interfaces) on one single address (including port). 如果您具有不同的功能,则始终可以定义一个以上ServiceContract(作为一个接口),并让您的服务类全部实现它们,并将一个服务类(带有所有ServiceContract接口)托管在一个地址(包括端口)上。

[ServiceContract]
public interface IMyService1
{
  [OperationContract]
  void SomeOperation1;

  [OperationContract]
  void SomeOperation2;
}

[ServiceContract]
public interface IMyService2
{
  [OperationContract]
  void SomeOperation21;

  [OperationContract]
  void SomeOperation22;
}

public class MyServiceClass : IMyService1, IMyService2
{
  void SomeOperation1;
  void SomeOperation2;
  void SomeOperation21;
  void SomeOperation22;
}

Marc

As @Koistya wrote it is possible. 正如@Koistya所写,这是可能的。 And here is a complete example: example . 这是一个完整的示例: example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM