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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. WCF Data Service 使用小结(二) —— 使用WCF Data Service 创建OData服务

    在 上一章 中,介绍了如何通过 OData 协议来访问 OData 服务提供的资源.下面来介绍如何创建一个 OData 服务.在这篇文章中,主要说明在.NET的环境下,如何使用 WCF Data Se ...

  4. 使用Microsoft Azure云平台中的Service Bus 中继 Intanet环境下的WCF服务。

    之前写的一篇文章:) 看起来好亲切. http://www.cnblogs.com/developersupport/archive/2013/05/23/WCF-ON-IIS-Azure-Servi ...

  5. 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 ...

  6. 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 ...

  7. Neutron LBaaS Service(2)—— Neutron Services Insertion Model

    Service Insertion Service Insertion是Neutron中实现L4/L7层服务的框架.Neutron以前只有一级插件结构用于实现各种L2层技术(如LinuxBridge, ...

  8. Open-Source Service Discovery

    Service discovery is a key component of most distributed systems and service oriented architectures. ...

  9. bluetooth service uuid

    转自:https://www.bluetooth.com/specifications/assigned-numbers/service-discovery service discovery ​​​ ...

随机推荐

  1. C#中的static静态变量的用法

    静态全局变量 定义:在全局变量前,加上关键字 static 该变量就被定义成为了一个静态全局变量. 特点: A.该变量在全局数据区分配内存. B.初始化:如果不显式初始化,那么将被隐式初始化为0. 静 ...

  2. <>跟!=

    这两个是没有区别的,都是不等于

  3. Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

    解决方法: Project -> Properties -> libraries, 先remove掉JRE System Library,然后再Add Library重新加入. ===== ...

  4. python基础(目录)

    1.数据库操作入门 2.网络编程入门 3.编码规范 4.测试

  5. 2014年10月30日-----SQL的基础知识

    数据库的概念 结构化查询语言:structured query language 简称:SQL 数据库管理系统:database management system 简称:DBMS 数据库管理员:da ...

  6. 百度前端技术学院(IFE)2016春季学期总结

    今天(5月16日)作为第八个提交者提交了任务五十:RIA微型问卷管理平台 这样一个综合性的大任务,宣告我的IFE春季学期课程学习顺利完成.其实任务五十并不复杂,现在再让我来做,可能一周不到就写出来了, ...

  7. web常用正则表达式

    1. 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2. "^\d+$" //非负整数(正整数 + 0) 3. "^[0-9]*[1-9] ...

  8. Python【第十篇】协程、异步IO

    大纲 Gevent协程 阻塞IO和非阻塞IO.同步IO和异步IO的区别 事件驱动.IO多路复用(select/poll/epoll) 1.协程 1.1协程的概念 协程,又称微线程,纤程.英文名Coro ...

  9. AngularJS: 自定义指令与控制器数据交互

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 网页端启动WinForm

    网页端启动WinForm 程序 在逛淘宝或者使用QQ相关的产品的时候,比如淘宝我要联系店家点击旺旺图标的时候能够自动启动阿里旺旺进行聊天.之前很奇怪为什么网页端能够自动启动客户端程序,最近在开发吉特仓 ...