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 ...
随机推荐
- mysql-distinct去重、mysql-group …
一.MYSQL-distinct用法 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记 ...
- media queries 媒体查询使用
media queries 翻译过来就是媒体查询,media 指的媒体类型.那么有哪些类型呢,常用的有 screen(屏幕).打印(print),个人理解就是它所在的不同终端. 常用的用法:1,< ...
- Ubuntu安装gevent
今天在安装包的过程中,按照网上的文章,出错,找了很久,最后才安装成功,希望能解决以后大家遇到的问题 Ubuntu安装gevent Gevent是一个基于greenlet的Python的并发框架,以赖于 ...
- 网页版 treeview使用中遇到的问题
<div class="ScrollBar" id="ItemsTree"></div> var cla = $("#Item ...
- 初学时的shell
学习期间写过一些shell脚本, 测试过程:vi test.sh 后把程序写入其中,保存退出.然后改变文件属性:chmod +x test.sh 最后执行:./test.shfor语句测试:1)#!/ ...
- fmt:formatDate标签的输出格式
http://blog.csdn.net/lidawei201/article/details/7197834
- ios&h5混合开发项目仿app页面跳转优化
前言:本人原本是ios开发工程师,但由于现今H5的兴起,行内刮起了一阵混合开发的风气,趁着这股劲,我也学了前端开发,不说研究的多深,但也能胜任日常的开发工作.长话短说,现今的混合开发应该还处于摸索阶段 ...
- DependencyProperty
<Window x:Class="DependencyPropertyDemo.MainWindow" xmlns="http://schemas.microsof ...
- MSSQL Server语句
随机从数据库中取出20条数据:select top 20 * from 表名 order by newid()
- 24种设计模式--命令模式【Command Pattern】
今天讲命令模式,这个模式从名字上看就很简单,命令嘛,老大发命令,小兵执行就是了,确实是这个意思,但是更深化了,用模式来描述真实世界的命令情况.正在看这本书的你,我猜测分为两类:已经工作的和没有工作的, ...