0

I am a bit confused on what exactly I am instantiating with the WCF ServiceHost when I have multiple endpoint contracts that I am adding to it.

The instantiation has included a typeof argument - which seems to be the contract and is so in everything I have read and done. However when I come across adding additional contracts - this is where my confusion about it is.

ServiceHost shost = new ServiceHost(typeof(MyService), NetTcpBinding, xyz);

So let's say I have several contracts - ProductService, BatchService, CustomerService these are endpoint contracts that each have an interface. Let's keep it simple there is an Add Method and a Get Method in each of these contracts.

I can then add these endpoints which are contracts to the ServiceHost ..

shost.Endpoint.Add(ProductService);
shost.Endpoint.Add(BatchService);
shost.Endpoint.Add(CustomerService);

This is my confusion if I create it with MyService, then does MyService need to incorporate the methods of all of my endpoint contracts or does this just pass in the first Endpoint Contract just to instantiate it and then all the additional ones are (forgive my lack of a better way of saying this) - additional services provided by the service that was instantiated with one of my endpoints ?

I have read on SO and looking here seems relevant and close - but does not give an explanation of the instantiation of the ServiceHost Run WCF ServiceHost with multiple contracts

I mean what is the point of instantiating the thing ; and then adding endpoints if you have to place all of the endpoint methods into the host contract anyway where btw you can specify namespaces for the contract as well..- that just seems so un oop .. is the answer found at the link really the viable answer (it smells WET ~ W'peat Every Thing - AKA not DRY].

Community
  • 1
  • 1
Ken
  • 2,518
  • 2
  • 27
  • 35
  • @Tim aren't the endpoints contracts in and of themselves ? – Ken Feb 08 '16 at 21:12
  • @Tim yes I remember ABC - Address, Binding, [Contract].. So then why not instantiate the host with out a contract - these additional contracts they are in fact contracts. The Host has a Contract - but specifically what is the contract really - is it all my other contracts lumped into one .. the answer at the link seems to state that. That leads me to say why add the endpoints as separate endpoints - I can namespace with WCF annotations.. of course that is not what I am after - I have separate contracts and do not wish to add them all into my main ServiceHost - as 1 just does not make sense. – Ken Feb 08 '16 at 21:20
  • @Tim: you can have **ONE service implementation** per ServiceHost - but that service class can implement **multiple contracts** - so your statement is factually wrong – marc_s Feb 08 '16 at 21:24
  • @marc_s - yes the service implementation - what is the point in this - if I have to add all of my contracts methods to the service implementation - I understand the endpoints allow definition of different bindings. And so it seems oop logical that I instantiate the ServiceHost add my contracts and I am good to go - but the link says otherwise that I must add these things to the service implementation .. I am missing something or is it WCF has a reason to its madness.. – Ken Feb 08 '16 at 21:29
  • WCF is **not** OOP ! WCF is quite a separate, different world ! WCF also doesn't support things like generics or methods that differ only in their parameters - WCF is very much a **service** world and that's not always OOP ! – marc_s Feb 08 '16 at 21:30

1 Answers1

2

The ServiceHost can host one service - that is one service class (implementation class). But that single class can implement multiple WCF service contracts.

So if you have three service contracts (as interfaces IProductService, IBatchService, ICustomerService) and a single class MyServiceClass which implements all three of those interface contracts

public class MyServiceClass : IBatchService, ICustomerService, IProductService

then you can host this class in a ServiceHost and you can define endpoints for each of those three service contracts.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • So literally it is like the link; no wonder people steer to data services when dealing with databases ; in my case the limitations of no tcp binding, no duplex contracts - ends that choice for me. – Ken Feb 08 '16 at 21:33
  • 2
    WCF is a highly flexible, XML-based messaging service - that basic architecture does have a number of ramifications, which might (or might not) suit your application's need - if it's not your thing, then use something else - but WCF **IS** an extremely flexible, extremely well engineered product with very strong and compelling plus points in an enterprise architecture environment – marc_s Feb 08 '16 at 21:35