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. ARM9嵌入式学习笔记(2)-Vi使用

    ARM9嵌入式学习笔记(2) 实验1-1-3 Vi使用 vi创建文件vi hello.c:vi smb.conf-打开文件smb.conf i键-插入模式:esc键-命令行模式::-底行模式: 底行模 ...

  2. 虚拟化之KVM virsh常用命令篇

    1,查看运行的虚拟机 virsh list 2,查看所有的虚拟机(关闭和运行的虚拟机) virsh list --all 3,连接虚拟机 virsh console +域名(虚拟机的名称) 4,退出虚 ...

  3. office 文件在网页中显示

    1.如何在网页上显示word和excel a.可以使用office组件或aspose将word 和excel 转换为pdf 然后在网页上打开pdf,但是效果不是很好 .比如说excel 多个工作薄不是 ...

  4. npm创建和发布模块

    今天项目需要使用npm去创建一个模块,然后我查询了了npm的使用文档(Working with private modules),然后对其进行了整理. 一.在操作之前,我们首先要将npm装好,并且登录 ...

  5. #ifndef #define #endif 防止头文件被重复引用

    想必很多人都看过“头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?是不能在不同的两个文件中使用include来包含这个头文件 ...

  6. python给多个发送邮件附件,参考于《python自动化运维》

    #!/usr/bin/env python #coding: utf-8 #author:luodi date:2015/02/12 #description:this is a send mail ...

  7. phonegap退出android程序

    最近用android做了一个程序,在点“后退”的时候,会不停地后退,感觉不好. 查了些资料有这么些: 一.toast_plugin插件 <script type="text/javas ...

  8. Android上使用OpenglES2.0遇到的一点问题

    按照教程开发OpenglES2.0应用,遇到Logcat报错“Called unimplemented OpenGL ES API” 在论坛和stackoverflow上找到了答案. 1.manife ...

  9. android textView 富文本显示

    String parenName = entity.getParent().getMember_nickname(); parantNameRich = "<font color='# ...

  10. PCB電路板為何要有測試點?

    對學電子的人來說,在電路板上設置測試點(test point)是在自然不過的事了,可是對學機械的人來說,測試點是什麼?可能多還有點一頭霧水了.我記得我第一次進電子組裝廠工作當製程工程師的時候,還曾經為 ...