利用反射,批量启动WCF服务
对于WCF的宿主启动来说,有好多方法,单独启动也很简单,可以根据业务需要来自由选择(单独启动方法这里就不做解释)
对于业务服务比较多的时候,往往需要多个服务来承载系统,但是如果将服务启动单独写代码启动的话,这样就造成代码的耦合,增加服务,删除服务都需要对宿主程序进行修改编译,因而就需要一个批量启动的办法
现在着重整理一下理由反射批量启动
思路:1、自定义两个属性,用于接口和实现类,一个为接口属性,一个为实现类属性
2、利用反射获取指定程序集的所有类文件,根据标记属性可以获取到那些为WCF接口和那些为WCF实现类
3、可以通过反射获取到实现类继承与什么接口
4、根据前三项即可将指定程序集的接口类和实现类对应
具体代码如下:
属性:
- namespace ESTM.WCF.Service
- {
- //标记此特性的为WCF服务接口
- public class ServiceInterfaceAttribute : Attribute
- {
- }
- //标记此特性的为WCF服务接口实现类
- public class ServiceClassAttribute : Attribute
- {
- }
- }
接口和实现
- namespace ESTM.WCF.Service
- {
- /// <summary>
- /// 工厂布局模块接口契约
- /// </summary>
- [ServiceInterface]
- [ServiceContract]
- public interface IFactoryLayoutWCFService
- {
- [OperationContract]
- List<DTO_TM_PLANT> GetAllPlant();
- }
- }
- namespace ESTM.WCF.Service
- {
- [ServiceClass]
- public class FactoryLayoutWCFService : IFactoryLayoutWCFService
- {
- public List<DTO_TM_PLANT> GetAllPlant()
- {
- throw new NotImplementedException();
- }
- }
- }
WCF 批量启动帮助类
- namespace ESTM.WCF.Service
- {
- public class Bootstrapper
- {
- private string strBaseServiceUrl = ConfigurationManager.AppSettings["ServiceUrl"].ToString();
- //启动所有的服务
- public void StartServices()
- {
- //1.读取此程序集里面的有服务契约的接口和实现类
- var assembly = Assembly.Load(typeof(Bootstrapper).Namespace);
- //获取当前程序集的所有类文件(包括接口和类)
- var lstType = assembly.GetTypes();
- //存储当前程序集的所有接口
- var lstTypeInterface = new List<Type>();
- //存储当前程序集的所有接口实现类
- var lstTypeClass = new List<Type>();
- foreach (var oType in lstType)
- {
- //2.通过接口上的特性取到需要的接口和实现类
- var lstCustomAttr = oType.CustomAttributes;
- //如果当前类上面存在属性标签
- if (lstCustomAttr.Count() <= )
- {
- continue;
- }
- //获取第一个属性标签,并且判断是否相等于接口自定义属性
- //如果相等,则为接口
- var oInterfaceServiceAttribute = lstCustomAttr.FirstOrDefault(x => x.AttributeType.Equals(typeof(ServiceInterfaceAttribute)));
- if (oInterfaceServiceAttribute != null)
- {
- lstTypeInterface.Add(oType);
- continue;
- }
- //如果相等,则为类
- var oClassServiceAttribute = lstCustomAttr.FirstOrDefault(x => x.AttributeType.Equals(typeof(ServiceClassAttribute)));
- if (oClassServiceAttribute != null)
- {
- lstTypeClass.Add(oType);
- }
- }
- //3.启动所有服务
- foreach (var oInterfaceType in lstTypeInterface)
- {
- //在实现类集合中 获取由当前 Type 实现或继承的特定接口。
- var lstTypeClassTmp = lstTypeClass.Where(x => x.GetInterface(oInterfaceType.Name) != null).ToList();
- if (lstTypeClassTmp.Count <= )
- {
- continue;
- }
- //如果当前类获取到的接口等于遍历的接口名称,则匹配成功,
- if(lstTypeClassTmp[].GetInterface(oInterfaceType.Name).Equals(oInterfaceType))
- {
- var oTask = Task.Factory.StartNew(() =>
- {
- OpenService(strBaseServiceUrl + "/" + oInterfaceType.Name, oInterfaceType, lstTypeClassTmp[]);
- });
- }
- }
- }
- //通过服务接口类型和实现类型启动WCF服务
- private void OpenService(string strServiceUrl, Type typeInterface, Type typeclass)
- {
- Uri httpAddress = new Uri(strServiceUrl);
- using (ServiceHost host = new ServiceHost(typeclass))//需要添加System.SystemModel这个dll。。。。CSOAService这个为实现ICSOAService的实现类,WCF真正的实现方法再这个类里面
- {
- ///////////////////////////////////////添加服务节点///////////////////////////////////////////////////
- host.AddServiceEndpoint(typeInterface, new WSHttpBinding(), httpAddress);//ICSOAService这个为向外暴露的接口
- if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
- {
- ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
- behavior.HttpGetEnabled = true;
- behavior.HttpGetUrl = httpAddress;
- host.Description.Behaviors.Add(behavior);
- }
- host.Opened += delegate
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("服务启动成功。服务地址:" + strServiceUrl);
- };
- host.Open();
- while (true)
- {
- Console.ReadLine();
- }
- }
- }
- }
- }
利用反射,批量启动WCF服务的更多相关文章
- 普通用户如何启动WCF服务
做Winform项目时,部署到客户机上有两个应用程序,Host和Client,在Host上运行着WCF服务供Client调用.平时现在在测试的时候都没发现有问题,但是当安装到客户的正式环境时发现服务启 ...
- WCF全面解析之三 使用配置文件启动WCF服务
知识:WCF地址.WCF绑定 Endpoint的配置 服务的三要素(ABC) A:Address 地址 有传输方式信息 B:Binding 怎么做(与地址的传输方式要匹配) C:Contract 做什 ...
- 启动WCF多个服务方法
引用就不说明,直接贴上: using System;using System.Collections.Generic;using System.Linq;using System.Text;using ...
- 编写寄宿于windows服务的WCF服务
由于业务中有些任务需要在后台静默长期运行,或者有些服务队响应的要求比较苛刻,这样的WCF服务就不适合寄宿于IIS中.IIS每隔一段时间w3wp进程会闲置超时,造成服务的运行停止,因此这种耗时或者定时任 ...
- WCF服务使用(IIS+Http)和(Winform宿主+Tcp)两种方式进行发布
1.写在前面 刚接触WCF不久,有很多地方知其然不知其所以然.当我在[创建服务->发布服务->使用服务]这一过程出现过许多问题.如客户端找不到服务引用:客户端只在本机环境中才能访问服务,移 ...
- WCF服务端调用client.
wcf服务端 1,新建一个"windows窗口程序"名称为WCFServer2. 2.然后加入一个"WCF服务"名称为Service1. 详细步骤为:解决方式试 ...
- 记录:Web无引用无配置方式动态调用WCF服务
这几年一直用WebApi较多,最近项目中有个需求比较适合使用WCF,以前也用过JQuery直接调用Wcf的,但是说实话真的忘了… 所以这次解决完还是花几分钟记录一下 WCF服务端:宿主在现有Win服务 ...
- WCF学习之旅—WCF服务的批量寄宿(十三)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) WCF学习之旅—WCF ...
- 三十、【C#.Net开发框架】WCFHosting服务主机的利用WCF服务通讯和实现思路
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...
随机推荐
- 前端分享----JS异步编程+ES6箭头函数
前端分享----JS异步编程+ES6箭头函数 ##概述Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只 ...
- python docker库
安装方式pip pip install docker -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 官方文档地址 ht ...
- Day 2 Python 基础数据类型
2.os.path.join()函数 语法: os.path.join(path1[,path2[,......]]) 返回值:将多个路径组合后返回 注:第一个绝对路径之前的参数将被忽略 1 2 3 ...
- session和cookie的异同
Cookie是服务器发给客户端的一小段文本,保存在浏览器所在客户端的内存和磁盘上.服务器可以从客户端读出这些cookie.通过cookie,客户端可以和服务器端建立起一种联系,也就是说,Cookie是 ...
- Weekly Contest 132
1025. Divisor Game Alice and Bob take turns playing a game, with Alice starting first. Initially, th ...
- Storm集群参数调整
Supervisor 参数调整 修改${STORM_HOME}conf/storm.yaml文件内容 supervisor变更参数 slots 配置: 若storm host仅仅执行superviso ...
- SELECT 三级联动 [转]
<!DOCTYPE html> <html> <head> <meta charset=gbk /> <title>selectList&l ...
- iOS-实现对象拷贝【对象拷贝】
对象引用 NSCopying 代理 .h @interface xk : NSObject <NSCopying> @property (nonatomic, copy) NSString ...
- 下载一个vue项目执行npm install 后运行项目npm run dev后出错 - 问题解决
在SVN上拉下来一个vue项目,上面没有提交项目里面的node_modules文件夹,所以要自己执行 npm install 安装,但安装完后运行项目后却报错了: $ npm run dev > ...
- Storm系列一: Storm初步
初入Storm 前言 学习Storm已经有两周左右的时间,但是认真来说学习过程确实是零零散散,遇到问题去百度一下,找到新概念再次学习,在这样的一个循环又不成体系的过程中不断学习Storm. 前人栽树, ...