Hosting Multiple Service Implementations On The Same Port With WCF

來源:互聯網
上載者:User

標籤:

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();

 

Hosting Multiple Service Implementations On The Same Port With WCF

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.