Hosting Multiple Service Implementations On The Same Port With WCF
2015-08-04 13:41
671 查看
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:
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();
相关文章推荐
- 英女子每天嚼7小时口香糖 终致嘴受损张不开
- 百度地图API - 行车路线 轨迹
- Html页面加回滚
- Java开发中常见的异常问题
- python的一些总结3
- hello world 模块编译
- 为Android添加一门新语言
- 七牛是如何搞定每天500亿条日志的
- 泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink
- Java容器
- 泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink
- Xcode7真机调试
- 开始新路程了,配置Android开发环境
- 读书笔记之c和指针(1)
- 使用JAX-WS开发WebService简单入门(1)
- bootstrap( 导航条、分页导航)
- Qt 常用类 (7)—— QPixmap
- (转)MEF+Unity
- git rev tips
- spark中的SparkContext实例的textFile使用的小技巧