wcf中如何Host多个WCF服务?
如果用self-host的方式来加载服务的话,我们一般是用这样的代码:ServiceHost host1 = new ServiceHost(typeof(UserService)); 问题是,如果当你的服务很多的时候这样做是不胜其烦的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的时候,看到了他解决这一问题的方法:
一.服务配置在app.config或web.config中:
2using System.Collections.Generic;
3using System.Reflection;
4using System.Configuration;
5using System.ServiceModel.Configuration;
6using System.ServiceModel;
7
8public class ServiceHostGroup
9{
10 static List<ServiceHost> _hosts = new List<ServiceHost>();
11
12 private static void OpenHost(Type t)
13 {
14 ServiceHost hst = new ServiceHost(t);
15 hst.Open();
16 _hosts.Add(hst);
17 }
18
19 public static void StartAllConfiguredServices()
20 {
21 Configuration conf =
22 ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
23
24 ServiceModelSectionGroup svcmod =
25 (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
26 foreach (ServiceElement el in svcmod.Services.Services)
27 {
28 Type svcType = Type.GetType(el.Name);
29 if (svcType == null)
30 throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
31 OpenHost(svcType);
32 }
33
34 }
35
36
37 public static void CloseAllServices()
38 {
39 foreach (ServiceHost hst in _hosts)
40 {
41 hst.Close();
42 }
43 }
44}
二.服务配置在外部配置文件中:
Services.XML:
<configuredServices>
<service type="ServiceImplementation.ArticleService, ServiceImplementation" />
</configuredServices>
ServiceHostBase.cs:
2using System.Collections.Generic;
3using System.Reflection;
4using System.Configuration;
5using System.ServiceModel.Configuration;
6using System.ServiceModel;
7using System.Xml;
8using System.Xml.Serialization;
9using System.IO;
10
11public class ServiceHostGroup
12{
13 static List<ServiceHost> _hosts = new List<ServiceHost>();
14
15 private static void OpenHost(Type t)
16 {
17 ServiceHost hst = new ServiceHost(t);
18 Type ty = hst.Description.ServiceType;
19 hst.Open();
20 _hosts.Add(hst);
21 }
22
23 public static void StartAllConfiguredServices()
24 {
25 ConfiguredServices services = ConfiguredServices.LoadFromFile("services.xml");
26
27 foreach (ConfiguredService svc in services.Services)
28 {
29 Type svcType = Type.GetType(svc.Type);
30 if (svcType == null) throw new Exception("Invalid Service Type " + svc.Type + " in configuration file.");
31 OpenHost(svcType);
32 }
33 }
34
35 public static void CloseAllServices()
36 {
37 foreach (ServiceHost hst in _hosts)
38 {
39 hst.Close();
40 }
41 }
42}
43
44[XmlRoot("configuredServices")]
45public class ConfiguredServices
46{
47 public static ConfiguredServices LoadFromFile(string filename)
48 {
49 if (!File.Exists(filename)) return new ConfiguredServices();
50
51 XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices));
52 using (FileStream fs = File.OpenRead(filename))
53 {
54 return (ConfiguredServices) ser.Deserialize(fs);
55 }
56 }
57
58 [XmlElement("service", typeof(ConfiguredService))]
59 public List<ConfiguredService> Services = new List<ConfiguredService>();
60}
61
62public class ConfiguredService
63{
64 [XmlAttribute("type")]
65 public string Type;
66}
原文:
Start ServiceHosts for all configured Services
wcf中如何Host多个WCF服务?相关推荐
- 数字证书及在WCF中的应用
一 概念 1.内容 证书的发布机构 证书的有效期 证书所有者(Subject) 签名所使用的算法 指纹以及指纹算法 公钥 私钥 2.存储区 3.有效性 二 作用 ...
- wcf中的使用全双工通信(转)
wcf中的使用全双工通信 wcf中的契约通信默认是请求恢复的方式,当客户端发出请求后,一直到服务端回复时,才可以继续执行下面的代码. 除了使用请求应答方式的通信外,还可以使用全双工.下面给出例子: 1 ...
- WCF中常见的几种Host,承载WCF服务的方法详解
1:写在前面 我们都知道WCF在运行的时候必须自己提供宿主来承载服务.WCF 本身没有附带宿主,而是提供了一个 ServiceHost 的类,该类允许您在自己的应用程序中host WCF 服务.然后调 ...
- WCF中常见的几种Host,承载WCF服务的方法
1:写在前面 我们都知道WCF在运行的时候必须自己提供宿主来承载服务.WCF 本身没有附带宿主,而是提供了一个 ServiceHost 的类,该类允许您在自己的应用程序中host WCF 服务.然后调 ...
- 在WCF中使用Ninject轻量级IOC框架 之 SOAP风格服务
最近学习MVC 看到很多文章都用了Ninject框架进行解耦,就考虑是否能用在平时写的WCF服务中,因为毕竟目前还是总要写服务的--蛋疼ing-- 传送门: Ninject框架官网: http://w ...
- 在WCF中实现双工通信(转载)
首先声明此文章是转载博客园蒋老师之作:http://www.cnblogs.com/artech/archive/2007/03/02/661969.html 双工(Duplex)模式的消息交互方式体 ...
- WCF中Service Configuration Editor的使用方法(转)
本文转自: http://www.cnblogs.com/pdfw/archive/2007/12/25/1014265.html 1.在App.config文件上右击,选择Edit WCF Conf ...
- WCF中绑定的简单介绍
绑定基本概念 绑定就是一个从通用基础类型派生出来的运行时类型.绑定中描述了传输协议,消息编码格式和其他的一些用于通信的通信协议. 绑定的种类介绍 类型名 配置文件使用名 描述 BasicHttpBin ...
- 【转】x.509证书在WCF中的应用(CS篇)
[转自]x.509证书在WCF中的应用(CS篇) 为什么要用x.509证书? WCF的服务端和客户端之间,如 果不作任何安全处理(即服务端的<security mode="None&q ...
最新文章
- 和12岁小同志搞创客开发:如何使用继电器?
- 7 Papers Radios | 机器人「造孩子」;谷歌裸眼3D全息视频聊天技术公开
- pytorch方法测试——损失函数(CrossEntropyLoss)
- THttprio连接WebService的内存泄漏问题
- mysql直连1.执行语句_MySQL随笔01_一条SQL语句是如何执行的
- ant build里如何指定classpath
- map怎么转化dto_阿里面试题:为什么Map桶中个数超过8才转为红黑树
- charles抓包ios抓拍教程
- Python音频信号处理 2.使用谱减法去除音频底噪
- Android问题-selection contains a component,button7,introduced in an ancestor and cannot be deleted....
- UOS (deepin)刻录u盘安装系统
- Postman的测试脚本(一)
- Linux常用快捷键命令
- 机器人感知与规划笔记 (7) - 行为架构 (Behavioral Architectures)
- GPRS/UMTS分组域漫游(转)
- VAE 中后验坍塌问题
- 安卓zip解压软件_纯C语言编写的开源免费解压压缩软件——拥有极致性能的 7-zip...
- linux系统命令-查看内存使用情况
- HDU 5445 Food Problem(多重背包)
- 求100以内的素数并输出(详细讲解)
热门文章
- dva处理_dva中使用store管理数据的异步问题
- mysql count null_一个不可思议的MySQL慢查分析与解决
- python标准库说明_Python标准库详细介绍与基本使用方式,超详细!
- php output详解,【PHP】Output Control 扩展详细解读
- mysql行锁怎么读_MySQL锁(三)行锁:幻读是什么?如何解决幻读?
- 感知器(Perceptron)
- tomcat一段时间不操作oracle就关闭连接_操作数据库常见错误,开发人员必掌握的技能...
- Storey FDR矫正方法
- 系统学习深度学习(五) --递归神经网络原理,实现及应用
- 项目管理实践之版本控制工具SVN