简体   繁体   中英

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.

I will host the service(s) in a Windows Service (so with WCF I'd using 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.

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

Also, I do agree with Joe's logic of when it's appropriate to implement multiple service contracts on one concrete class.

A service can expose multiple Service Contracts.

I would create WCF service implemented as one class, which itself implements several interfaces with [ServiceContract] attributes.

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 . 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. 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]
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. And here is a complete example: example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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