C#(VS2008)服务编写-安装和部署
1.创建一个空白解决方案。
2.在解决方案下面添加两个Windows服务:WXSmsGuardNew(保护服务),WXSmsMainNew(主服务).

3.第一个服务作为保护服务,服务上添加两个控件:System.Timers.Timer和System.ServiceProcess.ServiceController
由于System.Timers.Timer不会显示在工具栏中无法直接拖到服务中,故而我们找到服务设计器页面WXSmsGuardNew.Designer.cs,将手动构造System.Timers.Timer控件
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
this.sc = new System.ServiceProcess.ServiceController();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = ;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// sc
//
this.sc.ServiceName = "WXSmsGuardNew";
//
// WXSmsGuardNew
//
this.ServiceName = "WXSmsGuardNew";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
#endregion
private System.Timers.Timer timer1;
private System.ServiceProcess.ServiceController sc;
将 ServiceController控件的属性Name改为"sc",ServiceName改为"WXSmsMainNew",因为着这个控件是跟踪主服务用的所以设置为主服务的服务名
保护服务中,Timer监控主服务运行状态
public WXSmsGuardNew()
{
//验证服务器证书回调自动验证
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
InitializeComponent();
}
public bool RemoteCertificateValidate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // 总是接受
return true;
}
protected override void OnStart(string[] args)
{
WriteLog("保护服务已开启");
} protected override void OnStop()
{
WriteLog("保护服务已关闭");
} private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer1.Enabled = false;
sc.Refresh();
if (sc.Status != ServiceControllerStatus.Running)
{
sc.Start();
WriteLog("发现发送已关闭,已经开启");
}
timer1.Enabled = true;
}
主服务则负责专门做需要做的事情,如实时发送短信,或者其他逻辑
public WXSmsMainNew()
{
//验证服务器证书回调自动验证
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
InitializeComponent();
}
public bool RemoteCertificateValidate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // 总是接受
return true;
}
protected override void OnStart(string[] args)
{
WriteLog("发送服务已开启");
} protected override void OnStop()
{
WriteLog("发送服务已关闭");
} #region 获取参数
public string GetConfig(string key)
{
return ConfigurationManager.AppSettings[key];
} public string GetConfig(int index)
{
return ConfigurationManager.ConnectionStrings[index].ConnectionString;
} public int GetConfigCont()
{
return ConfigurationManager.ConnectionStrings.Count;
}
#endregion #region timer 事件
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer1.Enabled = false; int num = GetConfigCont();
for (int i = ; i < num; i++)
{
SendSms ssms = new SendSms();
bool b = ssms.SendWX(GetConfig(i));
if (!b)
{
this.Stop();
}
} timer1.Enabled = true;
}
#endregion
辅助方法,记录日志
#region 写日志
/// <summary>
/// 写日志
/// </summary>
/// <param name="msg">日志内容</param>
public void WriteLog(string msg)
{
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\log\\";
Directory.CreateDirectory(path);
for (int i = ; ; i++)
{
string fileName = string.Format("{0}WXSendServer{1}-{2}.txt", path, DateTime.Now.ToString("yyyyMMdd"), i);
if (File.Exists(fileName))
{
FileInfo fileInfo = new FileInfo(fileName);
if (fileInfo.Length >= * )//>=2M
{
continue;
}
}
string msgInfo = string.Format("{0} {1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msg);
File.AppendAllText(fileName, msgInfo);
break;
}
}
#endregion
以上完成了代码编写。
4.然后配置安装及服务信息
在两个服务的设计界面点鼠标右键有个"添加安装程序",点击之后会生成ProjectInstaller.cs文件,将此文件自动生成的控件于服务相对应


5.安装和部署
解决方案添加安装项目

在安装项目上右键--视图--文件系统中,右键--添加--项目输出,将两个服务均作为主输出添加进去

然后在安装项目上右键--视图--自定义操作,将两个主输出均添加到对应的步骤下.

最后,生成安装项目,会在安装项目的目录下生成两个文件,安装其中任何一个均可

C#(VS2008)服务编写-安装和部署的更多相关文章
- resin服务之一---安装及部署
参考网站: http://caucho.com/ http://www.oschina.net/p/resin http://caucho.com/resin-4.0/admin/starting-r ...
- C# windows 服务编写及安装
最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里 ...
- Burrow 服务的安装部署
Burrow 服务的安装部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近协助开发的同时帮忙把10个topic的数据使用5个topic的来工作.结果发现数据flume在手机数 ...
- Zabbix 3.4 服务端安装部署
关于zabbix的安装部署官方也提供了详细的安装文档,链接如下: https://www.zabbix.com/download 选择zabbix的版本,服务器平台及使用的数据库 安装和配置zabbi ...
- 编写Windows服务疑问2:探索服务与安装器的关系
首先,来弄两个服务,一个叫“飞机”,一个叫“火车”. public class FeiJiService : ServiceBase { public FeiJiService() { Service ...
- ElasticSearch安装及部署
安装及部署 一.环境配置 操作系统:Cent OS 7ElasticSearch版本:1.3.2JDK版本:1.7.0_51SSH Secure Shell版本:XShell 5elasticsear ...
- 一、MongoDB的下载、安装与部署
1.什么是MongoDB? 它是介于关系型数据库和非关系型数据库之间的一种NoSQL数据库,用C++编写,是一款集敏捷性.可伸缩性.扩展性于一身的高性能的面向文档的通用数据库. 2.为什么要用Mong ...
- ZooKeeper的安装与部署
本文讲述如何安装和部署ZooKeeper. 一.系统要求 ZooKeeper可以运行在多种系统平台上面,表1展示了zk支持的系统平台,以及在该平台上是否支持开发环境或者生产环境. 表1:ZooKeep ...
- centos7下zabbix安装与部署
1.Zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...
随机推荐
- Codevs1992题解
题目大意 求有向图中经过某一点k的最大环(数据规模不支持floyd). 题解 以k为起点在正向图中spfa求单源最短路.再在反向图中spfa求单源最短路. 枚举除k外的每个点i.假设有一个同一时候包括 ...
- 80端口被NT kernel & System 占用pid= 4的解决方法
引用http://www.2cto.com/os/201111/111269.html的方法.亲测可用 该进程是Http.sys.它是http API的驱动组件,Http栈服务器.如果该端口被Http ...
- css权重计算
第一等:代表内联样式,如: style=””,权值为1000. 第二等:代表ID选择器,如:#content,权值为100. 第三等:代表类,伪类和属性选择器,如.content,权值为1 ...
- 3D模型制作
agisoft: http://www.agisoft.com/downloads/installer/ http://pan.baidu.com/s/1dDwA3tf http://pan.baid ...
- HTML5 drawImage 使用问题
使用Image遇到的问题: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- JavaScript 数组操作备忘
数组的定义: 方法1. var mycars=new Array()mycars[0]="Saab"mycars[1]="Volvo"mycars[2]=&qu ...
- SQL Server 查看备份集元数据的 4 种方法。
方法 1. restore labelonly 方法 2. restore headeronly 方法 3. restore filelistonly 方法 4. restore verifyonly ...
- SARscape5.2哨兵1A数据的读取
SARscape5.2支持哨兵1A数据的读取,支持的数据类型有: SM SLC ——条带模式的斜距单视复数产品 IW SLC——干涉宽幅模式(TOPS Mode)的斜距单视复数产品 EW SLC——超 ...
- listview加载网络图片
ListView加载网络数据和图片 2013-09-25 00:08:10| 分类: 默认分类 | 标签:android |举报|字号 订阅 如,从服务器端获得商品名称.价格.简介和图片 ...
- wireshark的 rdp dissector
开源 wireshark的 rdp dissector https://github.com/boytm/wireshark_rdp. 1)support TCP reassemble 2)Fastp ...