Hosting Multiple Service Implementations On The Same Port With WCF
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();
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2014-08-04 关于消息队列