C#实现Windows后台服务实例浅析

开发 后端
C#实现Windows后台服务实例向你详细介绍了一个C#实现Windows后台服务的准备和实施的全过程,希望对你学习C#实现Windows后台服务有所帮助。

C#实现Windows后台服务实例之前要明白的一些概念:所谓Windows后台服务,即后台自动运行的程序,一般随操作系统启动而启动,在我的电脑 服务后应用程序 服务里面能看到当前电脑的服务.一般而言,程序上用VC、C++写Windows服务,但是我对这些语言不是很熟,一般编程用C#较多,所以就用C#语言写了一个Windows服务.

C#实现Windows后台服务实例其实需求是这样的,做那个报价系统的时候加入了发短信的功能,订单处理完即将发货的时候要发送短信都客户手机上,公司内部员工处理订单超时要自动发短信,群发产品促销信息到客户手机上等,还有定时发送短信的需求,所以***面决定把发短信的模块独立出来,以后还有什么功能方便一起调用,而最终选择了采用Windows后台服务.

C#实现Windows后台服务实例其实Windows服务并不好做到通用,它并不能在用户的界面显示一些什么信息等,它只是在后台默默的处理一些事情,起着辅助的作用.那如何实现发送段信通用调用的接口呢?它们之间的信息又是如何来交互呢?数据库!对,就是它存储数据信息的.而数据库都能很方便的访问操作.把发送短信的后台服务定时去访问一个数据库,而另外任何要发送短信的地方也访问数据库,并插入一条要发送的短信到表里面,稍后Windows后台服务访问该表将此短信发送出去.这可能是一个比较蠢的方法,但实现起来较简单.

C#实现Windows后台服务实例首先,由于它是要安装的,所以它运行的时候就需要一个安装类Installer将服务安装到计算机,新建一个后台服务安装类继承自Installer,安装初始化的时候是以容器进行安装的,所以还要建立ServiceProcessInstaller和ServiceInstaller服务信息组件添加到容器安装,在Installer类增加如下代码:

  1. private System.ComponentModel.IContainer components = null;  
  2. private System.ServiceProcess.ServiceProcessInstaller spInstaller;  
  3. private System.ServiceProcess.ServiceInstaller sInstaller;  
  4. private void InitializeComponent()  
  5. {  
  6. components = new System.ComponentModel.Container();  
  7.  
  8. // 创建ServiceProcessInstaller对象和ServiceInstaller对象  
  9. this.spInstaller = new System.ServiceProcess.
  10. ServiceProcessInstaller();  
  11. this.sInstaller = new System.ServiceProcess.ServiceInstaller();  
  12.  
  13. // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息  
  14. this.spInstaller.Account = System.ServiceProcess.
  15. ServiceAccount.LocalSystem;  
  16. this.spInstaller.Username = null;  
  17. this.spInstaller.Password = null;  
  18.  
  19. // 设定服务名称  
  20. this.sInstaller.ServiceName = "SendMessage";  
  21. sInstaller.DisplayName = "发送短信服务";  
  22. sInstaller.Description = "一个定时发送短信的服务";  
  23.  
  24. // 设定服务的启动方式  
  25. this.sInstaller.StartType = System.ServiceProcess.
  26. ServiceStartMode.Automatic;  
  27.  
  28. this.Installers.AddRange(new System.Configuration.
  29. Install.Installer[] { this.spInstaller, this.sInstaller });  

C#实现Windows后台服务实例再添加一个服务类继承自ServiceBase,我们可以重写基类的OnStart、OnPause、OnStop、OnContinue等方法来实现我们需要的功能并设置指定一些属性.由于是定事发送短信的服务,自然少不了Windows记时器,在OnStart事件里我们写入服务日志,并初始化记时器.

  1. private System.Timers.Timer time;  
  2. private static readonly string CurrentPath = 
  3. Application.StartupPath + "\\";  
  4. protected override void OnStart(string[] args)  
  5. {  
  6. string path = CurrentPath + "Log\\start-stop.log";  
  7. FileStream fs = new FileStream(path, FileMode.
  8. Append, FileAccess.Write);  
  9. StreamWriter sw = new StreamWriter(fs);  
  10. sw.WriteLine("The Service is Starting On " + 
  11. DateTime.Now.ToString());  
  12. sw.Flush();  
  13. sw.Close();  
  14. fs.Close();  
  15.  
  16. time = new System.Timers.Timer(1000 * Convert.
  17. ToInt32(GetSettings("TimeSpan")));  
  18. time.Enabled = true;  
  19. time.Elapsed += this.TimeOut;  
  20. time.Start();  

C#实现Windows后台服务实例实例化记时器类启动后,将在指定时间间隔触发Elapsed指定事件,如上GetSettings为读取我App.config文件里一个配置节点(值为30)的方法,所以上面将会每隔30秒调用TimeOut方法.而改方法就是我们发短信的具体操作.代码如下:

  1. private void TimeOut(object sender, EventArgs e)  
  2. {  
  3. try 
  4. {  
  5. if (GetSettings("Enabled").ToLower() == "true")  
  6. {  
  7. SqlConnection con = new SqlConnection(GetSettings("ConnString"));  
  8. SqlCommand cmd = new SqlCommand("select [sysid],
  9. [admin_inner_code],[user_inner_code],[phone],
  10. [message],[sendtime] from [tbl_note_outbox]", con);  
  11. con.Open();  
  12. SqlDataReader rdr = cmd.ExecuteReader();  
  13. while (rdr.Read())  
  14. {  
  15. string phone = rdr["phone"].ToString();  
  16. string message = rdr["message"].ToString();  
  17. string sendtime = rdr["sendtime"].ToString();  
  18. System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("GB2312");  
  19. string url = string.Format("http://211.155.23.205/
  20. isapi.dll?SendSms&AgentID={0}&PassWord={1}&phone={2}&msg={3}&sendtime={4}"
  21. GetSettings("AgentID"), GetSettings("PassWord"), 
  22. phone,System.Web.HttpUtility.UrlEncode( message,encoder), sendtime);  
  23. System.Net.WebClient wClient = new System.Net.WebClient();  
  24. string msg = System.Text.Encoding.Default.GetString(wClient.DownloadData(url));  
  25. wClient.Dispose();  
  26.  
  27. //删除已经发送成功的,并保存发送记录  
  28. if (msg == "发送成功")  
  29. {  
  30. DateTime dtsend = sendtime == "0" ? DateTime.Now : 
  31. DateTime.ParseExact(sendtime, "yyyyMMddHHmmss"null);  
  32. string sql = string.Format("delete from 
  33. [tbl_note_outbox] where [sysid]={0} INSERT INTO [tbl_note_log] 
  34. ([admin_inner_code],[user_inner_code],[status],[phone],
  35. [message],[sendtime]) VALUES('{1}','{2}','{3}','{4}','{5}','{6}')",
  36.  rdr["sysid"], rdr["admin_inner_code"], rdr["user_inner_code"],
  37.  msg, phone, message, dtsend);  
  38. SqlConnection conn = new SqlConnection(GetSettings("ConnString"));  
  39. SqlCommand delete = new SqlCommand(sql, conn);  
  40. conn.Open();  
  41. delete.ExecuteNonQuery();  
  42. conn.Close();  
  43. delete.Dispose();  
  44. }  
  45.  
  46. }  
  47. rdr.Close();  
  48. con.Close();  
  49. cmd.Dispose();  
  50. }  
  51. }  
  52. catch (Exception ex)  
  53. {  
  54. string errorPath = CurrentPath + "Log\\error.log";  
  55. if (!File.Exists(errorPath))  
  56. {  
  57. FileStream create = File.Create(errorPath);  
  58. create.Close();  
  59. }  
  60. FileStream fs = new FileStream(errorPath, 
  61. FileMode.Append, FileAccess.Write);  
  62. StreamWriter sw = new StreamWriter(fs);  
  63. sw.WriteLine("Exception: " +ex.Message+" --"+
  64.  DateTime.Now.ToString());  
  65. sw.Flush();  
  66. sw.Close();  
  67. fs.Close();  
  68. }  
  69.  

C#实现Windows后台服务实例上面我们使用try、catch访问数据库,并记录错误异常信息. 发送短信是使用发送一个Web请求发送出去的,要注意请求url字符串的编码类型,要与请求页面编码一致,不然会出现乱码.上面我们请求的是智网通集团短信(网址:http://www.09168.net/)的Web接口,通过访问他的网站来实现发短信,当然还要传递一些用户名、密码、手机号码和要发送的短信息等参数.他的收费平均大概为7分/条的样子,其实我原本不想用发送Web请求的这样方式来发送短信的,它本身提供了调用它发送短信的DLL,而且还有vc、delphi调用的Demo,但是没有用C#调用的例子,我刚开始试着用非托管动态链接库他提供的DLL,不知方法调用那里出错了一直都没能成功发送出短信,所以后来就用了他的Web方式接口了.他页面直接返回发送短信的状态信息.返回发送成功则短信发送成功,成功后我再将此条信息从要发送短信表里删除并保存在发送记录表里面,以备日后方便查询.其实登陆他的官网进入后台也能方便的查询,如下图.

保存在发送记录表里面 

C#实现Windows后台服务实例发送短信服务的代码基本上搞定了,就看怎么在服务器上安装部署了.C#写的Windows后台服务不能直接安装,需要借助.NET Framework里面的InstallUtil.exe安装工具安装,我们可以做成一个执行CMD命令的文件BAT文件来安装启动它,命令如下:

  1. %windir%\Microsoft.NET\  
  2. Framework\v2.0.50727\  
  3. InstallUtil.exe %CD%\  
  4. SendMessage.exe  
  5. net start SendMessage 

安装启动 

安装完成以后,我们可以在我的电脑管理服务里面看到才安装上的后台服务.

后台服务 

经测试,采用定时访问数据库发送短信的服务并不是很耗资源,刚启动的时候只占用内存为7、8M左右,经过在服务器上连续运行几天不关闭占用的内存也只升到15M左右,运行比较稳定,这里提供一个短信二次开发接口说明,有兴趣的朋友可以去下载看下.

智网动力集团短信二次开发说明文档示例

特别申明:本文及内容如非特别注明,均为本人Jonllen原创,版权均归原作者个人所有,转载必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

C#实现Windows后台服务实例的基本情况就向你介绍到这里,希望对你了解和学习C#实现Windows后台服务实例有所帮助。

【编辑推荐】

  1. C#windows服务状态改变操作浅析
  2. C#Windows服务程序开发实例介绍
  3. C#启动Windows服务及关闭实例实现
  4. C#启动Windows服务的窗体程序浅析
  5. C#Windows服务程序之安装项目
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-14 16:32:50

C#启动Windows

2009-08-27 18:09:49

C#接口的实现

2009-08-14 11:00:16

C#创建Windows

2009-08-27 13:30:11

C# interfac

2009-08-24 10:37:27

C# 泛型

2009-08-14 14:17:16

C#Windows服务

2009-08-14 16:02:50

C#启动windows

2009-08-17 17:49:20

C# 枚举

2009-09-09 13:57:28

C# XML解析

2009-08-18 13:49:21

C# 操作Excel

2009-08-27 17:59:56

C#接口定义

2009-12-11 13:59:35

F#

2009-08-14 09:50:46

C#复制构造函数

2009-08-31 15:11:23

C#调用水晶报表

2009-08-17 14:36:15

C#进度条实现

2009-08-14 16:41:22

C#启动Windows

2009-08-18 16:04:12

C# 操作Excel

2009-08-28 17:34:14

读取word文档

2009-08-27 17:11:44

C# Fluent I

2009-08-19 09:42:52

C#操作Word书签
点赞
收藏

51CTO技术栈公众号