tags:

views:

471

answers:

4

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.

+1  A: 

A service can expose multiple Service Contracts.

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

Koistya Navin
A: 

As @Koistya wrote it is possible. And here is a complete example: example.

empi
+1  A: 

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 (e.g. 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.

Joe
Thanks Joe. You say: "Not at all, you can have several service contracts on the same port." What are my options for implementing this? (Apart from @Koistya's suggesstion)
ChrisH
@Chris, why don't you want create multiple ServiceContract interfaces and one concrete implementation? (you can split this one class into several partial classes to better orgonize your projects structure) And the rest is done in .config file
Koistya Navin
There's nothing special to do to share ports, unless you want to share between processes (in which case google for Net.Tcp port sharing). I would normally host in IIS,to get benefits such as message-based activation etc. But when self-hosting you can create multiple ServiceHosts with the same port.
Joe
A: 

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

marc_s