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

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

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

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

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

private System.ComponentModel.IContainer components = null;  private System.ServiceProcess.ServiceProcessInstaller spInstaller;  private System.ServiceProcess.ServiceInstaller sInstaller;  private void InitializeComponent()  {  components = new System.ComponentModel.Container();   // 建立ServiceProcessInstaller对象和ServiceInstaller对象  this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();  this.sInstaller = new System.ServiceProcess.ServiceInstaller();   // 设定ServiceProcessInstaller对象的账号、用户名和密码等信息  this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;  this.spInstaller.Username = null;  this.spInstaller.Password = null;   // 设定服务名称  this.sInstaller.ServiceName = "SendMessage";  sInstaller.DisplayName = "发送短信服务";  sInstaller.Description = "一个定时发送短信的服务";   // 设定服务的启动方式  this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;   this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });  } C#实现Windows后台服务实例再添加一个服务类继承自ServiceBase,咱们能够重写基类的OnStart、OnPause、OnStop、OnContinue等方法来实现咱们须要的功能并设置指定一些属性.因为是定事发送短信的服务,天然少不了Windows记时器,在OnStart事件里咱们写入服务日志,并初始化记时器.api

private System.Timers.Timer time;  private static readonly string CurrentPath = Application.StartupPath + "\\";  protected override void OnStart(string[] args)  {  string path = CurrentPath + "Log\\start-stop.log";  FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);  StreamWriter sw = new StreamWriter(fs);  sw.WriteLine("The Service is Starting On " + DateTime.Now.ToString());  sw.Flush();  sw.Close();  fs.Close();   time = new System.Timers.Timer(1000 * Convert.ToInt32(GetSettings("TimeSpan")));  time.Enabled = true;  time.Elapsed += this.TimeOut;  time.Start();  } C#实现Windows后台服务实例实例化记时器类启动后,将在指定时间间隔触发Elapsed指定事件,如上GetSettings为读取我App.config文件里一个配置节点(值为30)的方法,因此上面将会每隔30秒调用TimeOut方法.而改方法就是咱们发短信的具体操做.代码以下:服务器

private void TimeOut(object sender, EventArgs e)  {  try {  if (GetSettings("Enabled").ToLower() == "true")  {  SqlConnection con = new SqlConnection(GetSettings("ConnString"));  SqlCommand cmd = new SqlCommand("select [sysid],[admin_inner_code],[user_inner_code],[phone],[message],[sendtime] from [tbl_note_outbox]", con);  con.Open();  SqlDataReader rdr = cmd.ExecuteReader();  while (rdr.Read())  {  string phone = rdr["phone"].ToString();  string message = rdr["message"].ToString();  string sendtime = rdr["sendtime"].ToString();  System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("GB2312");  string url = string.Format("http://211.155.23.205/isapi.dll?SendSms&AgentID={0}&PassWord={1}&phone={2}&msg={3}&sendtime={4}", GetSettings("AgentID"), GetSettings("PassWord"), phone,System.Web.HttpUtility.UrlEncode( message,encoder), sendtime);  System.Net.WebClient wClient = new System.Net.WebClient();  string msg = System.Text.Encoding.Default.GetString(wClient.DownloadData(url));  wClient.Dispose();   //删除已经发送成功的,并保存发送记录  if (msg == "发送成功")  {  DateTime dtsend = sendtime == "0" ? DateTime.Now : DateTime.ParseExact(sendtime, "yyyyMMddHHmmss", null);  string sql = string.Format("delete from [tbl_note_outbox] where [sysid]={0} INSERT INTO [tbl_note_log] ([admin_inner_code],[user_inner_code],[status],[phone],[message],[sendtime]) VALUES('{1}','{2}','{3}','{4}','{5}','{6}')", rdr["sysid"], rdr["admin_inner_code"], rdr["user_inner_code"], msg, phone, message, dtsend);  SqlConnection conn = new SqlConnection(GetSettings("ConnString"));  SqlCommand delete = new SqlCommand(sql, conn);  conn.Open();  delete.ExecuteNonQuery();  conn.Close();  delete.Dispose();  }   }  rdr.Close();  con.Close();  cmd.Dispose();  }  }  catch (Exception ex)  {  string errorPath = CurrentPath + "Log\\error.log";  if (!File.Exists(errorPath))  {  FileStream create = File.Create(errorPath);  create.Close();  }  FileStream fs = new FileStream(errorPath, FileMode.Append, FileAccess.Write);  StreamWriter sw = new StreamWriter(fs);  sw.WriteLine("Exception: " +ex.Message+" --"+ DateTime.Now.ToString());  sw.Flush();  sw.Close();  fs.Close();  }   } C#实现Windows后台服务实例上面咱们使用try、catch访问数据库,并记录错误异常信息. 发送短信是使用发送一个Web请求发送出去的,要注意请求url字符串的编码类型,要与请求页面编码一致,否则会出现乱码.上面咱们请求的是智网通集团短信(网址:http://www.09168.net/)的Web接口,经过访问他的网站来实现发短信,固然还要传递一些用户名、密码、手机号码和要发送的短信息等参数.他的收费平均大概为7分/条的样子,其实我本来不想用发送Web请求的这样方式来发送短信的,它自己提供了调用它发送短信的DLL,并且还有vc、delphi调用的Demo,可是没有用C#调用的例子,我刚开始试着用非托管动态连接库他提供的DLL,不知方法调用那里出错了一直都没能成功发送出短信,因此后来就用了他的Web方式接口了.他页面直接返回发送短信的状态信息.返回发送成功则短信发送成功,成功后我再将此条信息从要发送短信表里删除并保存在发送记录表里面,以备往后方便查询.其实登录他的官网进入后台也能方便的查询,以下图.ide

 

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

%windir%\Microsoft.NET\  Framework\v2.0.50727\  InstallUtil.exe %CD%\  SendMessage.exe  net start SendMessage 学习

安装完成之后,咱们能够在个人电脑管理服务里面看到才安装上的后台服务.测试

 

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

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

特别申明:本文及内容如非特别注明,均为本人Jonllen原创,版权均归原做者我的全部,转载必须保留此段声明源码天空,且在文章页面明显位置给出原文链接,不然保留追究法律责任的权利。

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

详细请参考:http://www.codesky.net/article/200908/128303.html

相关文章
相关标签/搜索