Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run WCF ServiceHost with multiple contracts

Running a ServiceHost with a single contract is working fine like this:

servicehost = new ServiceHost(typeof(MyService1)); servicehost.AddServiceEndpoint(typeof(IMyService1), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService1"); servicehost.Open(); 

Now I'd like to add a second (3rd, 4th, ...) contract. My first guess would be to just add more endpoints like this:

servicehost = new ServiceHost(typeof(MyService1)); servicehost.AddServiceEndpoint(typeof(IMyService1), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService1"); servicehost.AddServiceEndpoint(typeof(IMyService2), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService2"); servicehost.Open(); 

But of course this does not work, since in the creation of ServiceHost I can either pass MyService1 as parameter or MyService2 - so I can add a lot of endpoints to my service, but all have to use the same contract, since I only can provide one implementation?
I got the feeling I'm missing the point, here. Sure there must be some way to provide an implementation for every endpoint-contract I add, or not?

like image 777
Sam Avatar asked Dec 02 '08 16:12

Sam


People also ask

Is it possible for a WCF Service to implement multiple service contracts?

Sometimes in our mind the question arise; can we implement multiple service contract in WCF service? And the answer is, Yes we can.

Can WCF service have multiple endpoints?

As demonstrated in the Multiple Endpoints sample, a service can host multiple endpoints, each with different addresses and possibly also different bindings. This sample shows that it is possible to host multiple endpoints at the same address.

How many endpoints can a WCF Service have?

The service configuration has been modified to define two endpoints that support the ICalculator contract, but each at a different address using a different binding.

What is ServiceHost in WCF?

Implements the host used by the Windows Communication Foundation (WCF) service model programming model. Use the ServiceHost class to configure and expose a service for use by client applications when you are not using Internet Information Services (IIS) or Windows Activation Services (WAS) to expose a service.


1 Answers

You need to implement both services (interfaces) in the same class.

servicehost = new ServiceHost(typeof(WcfEntryPoint)); servicehost.Open();   public class WcfEntryPoint : IMyService1, IMyService2 {     #region IMyService1     #endregion      #region IMyService2     #endregion } 

FYI: I frequently use partial classes to make my host class code easier to read:

// WcfEntryPoint.IMyService1.cs public partial class WcfEntryPoint : IMyService1 {     // IMyService1 methods }  // WcfEntryPoint.IMyService2.cs public partial class WcfEntryPoint : IMyService2 {     // IMyService2 methods } 
like image 185
chilltemp Avatar answered Sep 25 '22 08:09

chilltemp