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. webapp 性能优化

    webapp 不像传统页面,它生命周期更长,在手机端上,硬件环境并没有pc上那么好.所以性能的优化尤为重要. webapp的性能优化主要分为两个方面 网络请求优化 和 页面渲染优化 , 我们对于性能优 ...

  2. linux下ACE使用epoll

    select和epoll的比较就不用多说了.ACE在linux下默认使用select来实现Reactor的.如何在linux下让ACE使用epoll. 1.加一个编译宏,告诉ACE不要使用默认的sel ...

  3. Arrays.copyof

    public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System. ...

  4. 如何查看SQLServer数据库每个表占用的空间大小?

    如何查看SQLServer数据库每个表占用的空间大小? 创建存储过程: CREATE PROCEDURE [dbo].[sys_viewTableSpace]AS BEGIN SET NOCOUNT ...

  5. HDU 5794 - A Simple Nim

    题意:    n堆石子,先拿光就赢,操作分为两种:        1.任意一堆中拿走任意颗石子        2.将任意一堆分成三小堆 ( 每堆至少一颗 )        分析:    答案为每一堆的 ...

  6. mysql数据库数据恢复方案概括总结

    方案一:(传统方案) 备份+binlog日志增量: 方案二:(针对update.delete语句忘加where的情况) Binlog日志文件中保存有错误操作之前和之后的两组数据,将错误操作之前的数据修 ...

  7. jquery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...

  8. 自动化高效css开发,畅谈less的灵活变化

    css是一种让html与样式分离解析而出现的代码,它的出现大大提高了程序员的工作效率,和后期进行维护的效率.但是发展至今,由于起死板单调的写法,越来越不能满足程序员们灵活的思维,很多时候是种恨铁不成钢 ...

  9. 混合使用Azure LB和ILB访问相同web服务(3)

    接下来我们来配置Azure Load balancer,就是面向公网的负载均衡器: 1.在该测试中,为了保持内网访问和外网访问一样的体验,本地端口和public端口和ILB一样,同样是80: PS C ...

  10. 厂商自定义USB设备固件程序及特性

    通过前面的学习,大家应该对USB固件程序结构有了比较深的认识,现在再来详细说说固件里决定设备识别成厂商自定义USB设备的地方有哪些,或者说厂商自定义USB设备的固件特性有哪些. 之前不止一次说过学习U ...