Hosting Multiple Service Implementations On The Same Port With WCF

本文介绍如何使用WCF在同一端口上部署多个服务实现。通过创建多个ServiceHost实例并为每个服务指定不同的完整URI,实现了IServiceA与IServiceB两个服务接口在同一端口的不同路径下运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/chucklu/p/4701778.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值