Hosting Multiple Service Implementations On The Same Port With WCF

时间:2021-03-09 18:10:15

Hosting Multiple Service Implementations On The Same Port With WCF

Recently I have been playing around with WCF and Visual Studio 2008.  I was building a set of web services.  The project consisted of two difference service interfaces (IServiceA and IServiceB) each with their own implementation class (ServiceA and ServiceB).  I wanted to host them on the same port:

http://localhost:8080/ServiceA

http://localhost:8080/ServiceB

I searched for an example of how to do this but came up empty. 

The problem is that when creating a new ServiceHost() you need to pass it a reference to the implementation class.  You could then add a service endpoint for each of the services using  AddServiceEndpoint(), passing in the interface for the respective service, but those are tied to the same single implementation class set when creating the ServiceHost().  To get around this, you could create a facade class that combines both implementations, but that seems kind of messy if you have lots of interfaces.

Another option is to create multiple service hosts.  One for each of the service implementations.  When doing this, you need to make sure not to set the BaseAddress Uri.  If you do, each ServiceHost has the same base address and an exception is thrown.  I got around this by not including a base URI when creating the service host and then specifying the full URI when adding the service endpoints.  This might not be the most ideal solution (for one thing, hardcoding the URI in the code is less than optimal, using the config file is the better way to go) but it worked for my simple project.

The code for creating the ServiceHost from my simple console application is listed below:

            Type serviceAServiceType = typeof(ServiceA);
            Type serviceAContractType = typeof(IServiceA);

            Type serviceBServiceType = typeof(ServiceB);
            Type serviceBContractType = typeof(IServiceB);

            ServiceHost serviceAHost = new ServiceHost(serviceAServiceType);
            ServiceHost serviceBHost = new ServiceHost(serviceBServiceType);

            // Add behavior for Services - enable WSDL access
            ServiceMetadataBehavior serviceABehavior = new ServiceMetadataBehavior();
            serviceABehavior.HttpGetEnabled = true;
            serviceABehavior.HttpGetUrl = new Uri("http://localhost:8080/ServiceA");
            serviceAHost.Description.Behaviors.Add(serviceABehavior);

            ServiceMetadataBehavior serviceBBehavior = new ServiceMetadataBehavior();
            serviceBBehavior.HttpGetEnabled = true;
            serviceBBehavior.HttpGetUrl = new Uri("http://localhost:8080/ServiceB");
            serviceBHost.Description.Behaviors.Add(serviceBBehavior);

            // Create basicHttpBinding endpoint at http://localhost:8080/ServiceA/  
            serviceAHost.AddServiceEndpoint(serviceAContractType, new BasicHttpBinding(), 
              "http://localhost:8080/ServiceA");
            // Create basicHttpBinding endpoint at http://localhost:8080/ServiceB/  
            serviceBHost.AddServiceEndpoint(serviceBContractType, new BasicHttpBinding(), 
              "http://localhost:8080/ServiceB");

            serviceAHost.Open();
            serviceBHost.Open();

            Console.WriteLine("Service is ready, press any key to terminate.");
            Console.ReadKey();