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();
Hosting Multiple Service Implementations On The Same Port With WCF的更多相关文章
- Learning WCF Chapter1 Hosting a Service in IIS
How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...
- Learning WCF Chapter1 Exposing Multiple Service Endpoints
So far in this chapter,I have shown you different ways to create services,how to expose a service en ...
- WCF Data Service 使用小结(二) —— 使用WCF Data Service 创建OData服务
在 上一章 中,介绍了如何通过 OData 协议来访问 OData 服务提供的资源.下面来介绍如何创建一个 OData 服务.在这篇文章中,主要说明在.NET的环境下,如何使用 WCF Data Se ...
- 使用Microsoft Azure云平台中的Service Bus 中继 Intanet环境下的WCF服务。
之前写的一篇文章:) 看起来好亲切. http://www.cnblogs.com/developersupport/archive/2013/05/23/WCF-ON-IIS-Azure-Servi ...
- Learning WCF Chapter1 Creating a New Service from Scratch
You’re about to be introduced to the WCF service. This lab isn’t your typical “Hello World”—it’s “He ...
- Service Station - An Introduction To RESTful Services With WCF
Learning about REST An Abstract Example Why Should You Care about REST? WCF and REST WebGetAttribute ...
- Neutron LBaaS Service(2)—— Neutron Services Insertion Model
Service Insertion Service Insertion是Neutron中实现L4/L7层服务的框架.Neutron以前只有一级插件结构用于实现各种L2层技术(如LinuxBridge, ...
- Open-Source Service Discovery
Service discovery is a key component of most distributed systems and service oriented architectures. ...
- bluetooth service uuid
转自:https://www.bluetooth.com/specifications/assigned-numbers/service-discovery service discovery ...
随机推荐
- Chrome浏览器离线下载地址(Stable/Beta/Dev)
最新稳定版:https://www.google.com/intl/zh-CN/chrome/browser/?standalone=1 最新测试版:https://www.google.com/in ...
- python 入门1
python的历史 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像Per ...
- Python教程:连接数据库,对数据进行增删改查操作
各位志同道合的同仁可以点击上方关注↑↑↑↑↑↑ 本教程致力于程序员快速掌握Python语言编程. 本文章内容是基于上次课程Python教程:操作数据库,MySql的安装详解 和python基础知识之上 ...
- 【原创教程】鲸吞HTML
首先,我们的angularJS课程分为三大模块: HTML/CSS/JS基础. angularJS详解. angualrJS的一些实用框架讲解. 其中,第一大模块的对象是对前端开发技术有点了解但不熟悉 ...
- 傻瓜式理解递归之php递归
写程序这么久了,有时候别人会问道一些算法比如排序啊,递归啊,总是不知道该怎么去说,今天就来整理一下,让更多的人去傻瓜式的理解递归.递归在网络上有很多定义,但有这么一句话听的最多:递归就是自己调用自己! ...
- (转)Spring 读书笔记-----使用Spring容器(一)
Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spr ...
- 消息中间件MQ基础理论知识
欢迎转载,转载请注明出处:http://www.cnblogs.com/lidabnu/p/5723280.html 消息中间件已经流行很长时间,一般情况下,不需要自己来从头研发.设计消息中间件,所以 ...
- TCP的服务流程
TCP协议提供的是可靠的.面向连接的传输控制协议,即在传输数据前要先建立逻辑连接,然后再传输数据,最后释放连接3个过程.TCP提供端到端.全双工通信:采用字节流方式,如果字节流太长,将其分段:提供紧急 ...
- Sqlserver 列转行 行转列
sqlserver的行转列 列转行问题 行转列:1 使用Case when 方式 CREATE TABLE [StudentScores]( [UserName] NVARCHAR(20), --学生 ...
- Cocos2d-x 学习资料收集
框架源代码: http://code.google.com/p/cocos2d-x/downloads/list 搭建环境 http://blog.csdn.net/ccf19881030/artic ...