利用反射,批量启动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 ...
随机推荐
- vs2017 xamarin新建单独UWP类库提示不兼容
One or more projects are incompatible with UAP,Version=v10.0 (win10-arm). One or more projects are i ...
- 文本比较算法Ⅱ——Needleman/Wunsch算法的C++实现【求最长公共子串(不需要连续)】
算法见:http://www.cnblogs.com/grenet/archive/2010/06/03/1750454.html 求最长公共子串(不需要连续) #include <stdio. ...
- 902. Numbers At Most N Given Digit Set
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- netstat 查看本机开放端口
root@kali:~# netstat -luntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Add ...
- 转载:在spring中嵌入activemq
转载:http://www.dev26.com/blog/article/137 web开发站中的邮件发送使用了activemq我这是从网上找的进行了一些修改,记录下来,为了避免发送邮件时程序对用户操 ...
- vue-router进阶笔记
导航守卫 vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航,大白话,检测路由跳转过程中的具体的变化. 一.全局前置守卫——router.beforeEach 使用router. ...
- [AIR] AIR程序调用本地默认应用程序打开本地文件
摘要: File类提供了一个方法openWithDefaultApplication可以用本地默认应用程序打开指定路径下的文件. 当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: v ...
- UITableView 的常用可复制代码
UITableView是使用中最常用的工具,下面列举一个常用的tableview类,以后直接复制代码,稍作修改,就能用了. #import "ViewController.h" @ ...
- 直接插入排序实现(Java)
直接插入排序介绍 直接插入排序的基本操作是将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表. 怎么理解呢?就是将n个待排序的元素看成一个有序表和一个无序表,开始时有序 ...
- POJ 1126
#include <stdio.h> #include <string> #include <iostream> using namespace std; int ...