Home Ask Login Register

Developers Planet

Your answer is one click away!

StixO February 2016

WCF ServiceHost instantiation confusion with multiple endpoint contracts

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


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


marc_s February 2016

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.

Post Status

Asked in February 2016
Viewed 1,247 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved