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能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...
随机推荐
- UBUNTU系统root帐号解锁
UBUNTU系统启动时默认以安装系统时输入的用户名进行登录.如:以非root帐号rusky登录系统,当我们做某些操作时系统提示需要输入root密码.但我们并没有设置过root密码.此时,可按如下操作为 ...
- escape和unescape给字符串编码
var before = "\xxx\xxx" var after = escape(before); var after2 = unescape(after );
- Activity之间通过intent 传递Map
//传递 Map<String,Object> data=orderlist.get(arg2-1); SerializableMap tmpmap=new SerializableMap ...
- HDU 1071 - The area
求曲线和直线围成的面积 求表达式,求积分 #include <iostream> using namespace std; ],y[]; int t; double k,m;//fx1: ...
- USACO 1.4 ariprog 解题报告
这是继虫洞之后又让我为难的一个 剪枝题目,无论如何,做的再快,也只能过6个点,最后三个点也TLE.后来参考了一下标答,大概思路是这样的. 朴素算法就不多说了,枚举a,b然后判断就行,网上说这样优化到位 ...
- vb的property 和event
Event 语句 定义用户自定义的事件. 语法 [Public] Event procedurename [(arglist)] Event 语句包含下面部分: 部分 描述 Public 可选的.指定 ...
- media type 与 media query
参考博客:http://www.qianduan.net/media-type-and-media-query.html media type(媒体类型)是css 2中的一个非常有用的属性,通过med ...
- Android Studio rename module Can't rename root module
Android Studio修改工程根目录的时候会报错, rename module Can't rename root module. 主要是该工程已经打开,再命名必须要关闭改工程,就跟正在写一个t ...
- MYSQL select ....outfile.....from.....
select .... outfile 'file_path' fields terminate by '\t' lines terminate by '\r\n' from table_name; ...
- 检测android的网络链接状态
http://www.oschina.net/question/100267_61129?sort=default&p=1#tags_nav http://www.cnblogs.com/to ...