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)服务编写-安装和部署的更多相关文章

  1. resin服务之一---安装及部署

    参考网站: http://caucho.com/ http://www.oschina.net/p/resin http://caucho.com/resin-4.0/admin/starting-r ...

  2. C# windows 服务编写及安装

      最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里 ...

  3. Burrow 服务的安装部署

    Burrow 服务的安装部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近协助开发的同时帮忙把10个topic的数据使用5个topic的来工作.结果发现数据flume在手机数 ...

  4. Zabbix 3.4 服务端安装部署

    关于zabbix的安装部署官方也提供了详细的安装文档,链接如下: https://www.zabbix.com/download 选择zabbix的版本,服务器平台及使用的数据库 安装和配置zabbi ...

  5. 编写Windows服务疑问2:探索服务与安装器的关系

    首先,来弄两个服务,一个叫“飞机”,一个叫“火车”. public class FeiJiService : ServiceBase { public FeiJiService() { Service ...

  6. ElasticSearch安装及部署

    安装及部署 一.环境配置 操作系统:Cent OS 7ElasticSearch版本:1.3.2JDK版本:1.7.0_51SSH Secure Shell版本:XShell 5elasticsear ...

  7. 一、MongoDB的下载、安装与部署

    1.什么是MongoDB? 它是介于关系型数据库和非关系型数据库之间的一种NoSQL数据库,用C++编写,是一款集敏捷性.可伸缩性.扩展性于一身的高性能的面向文档的通用数据库. 2.为什么要用Mong ...

  8. ZooKeeper的安装与部署

    本文讲述如何安装和部署ZooKeeper. 一.系统要求 ZooKeeper可以运行在多种系统平台上面,表1展示了zk支持的系统平台,以及在该平台上是否支持开发环境或者生产环境. 表1:ZooKeep ...

  9. centos7下zabbix安装与部署

    1.Zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...

随机推荐

  1. 那么温暖http合约,入门。

    简介 年提出.经过几年的使用与发展,得到不断地完好和扩展.眼下在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,并且HTTP-NG(Next Generation ...

  2. Reflux 使用教程

    Reflux是根据React的flux创建的单向数据流类库.Reflux的单向数据流模式主要由actions和stores组成.例如,当组件list新增item时,会调用actions的某个方法(如a ...

  3. AjaxHelper创建的ajax无效,JQuery直接方法post有效,原来是Microsoft.jQuery.Unobtrusive.Ajax错误,NuGet解决

    Get-Package -ListAvailable -Filter Microsoft.JQuery Microsoft.jQuery.Unobtrusive.Ajax –Version 3.2.0

  4. div添加透明边框透明背景css

    -webkit-box-shadow: 1px 0px 15px #696969; background: rgba(255, 255, 255, 0.45) none repeat scroll 0 ...

  5. Single Number,Single Number II

    Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...

  6. mysql 命令

    1.查看数据库: mysql> show databases; 2.使用数据库 mysql> use mysqldata; 3.查看数据表 mysql> show tables; 4 ...

  7. Arcgis Engine 添加一个Symbol符号样式步骤

    public static void DrawPictureMarkerSymbol(IGlobe globe, String layerName) { //添加一个图层 ESRI.ArcGIS.Ca ...

  8. 小希的迷宫--hdu1272(并查集)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. Big Number(大数)

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. Centos7网络配置+图形界面设置

    一. 查看网络地址: centos7取消了ifconfig命令,使用ip addr命令查看IP地址 二.配置网络 用VirtualBox安装的CentOS7,安装完成后,发现无法上网,于是到网上查了一 ...