




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C# Windows服务 定时执行访问数据库循环发送手机短信 WindowsC#C+C应用服务器文章来源:chrome:/newtab/所谓Windows后台服务,即后台自动运行的程序,一般随操作系统启动而启动,在我的电脑 服务后应用程序 服务里面能看到当前电脑的服务.一般而言,程序上用VC、C+写Windows服务,但是我对这些语言不是很熟,一般编程用C#较多,所以就用C#语言写了一个Windows服务.其实需求是这样的,做那个报价系统的时候加入了发短信的功能,订单处理完即将发货的时候要发送短信都客户手机上,公司内部员工处理订单超时要自动发短信,群发产品促销信息到客户手机上等,还有定时发送短信的需求,所以最后面决定把发短信的模块独立出来,以后还有什么功能方便一起调用,而最终选择了采用Windows后台服务.其实Windows服务并不好做到通用,它并不能在用户的界面显示一些什么信息等,它只是在后台默默的处理一些事情,起着辅助的作用.那如何实现发送段信通用调用的接口呢?它们之间的信息又是如何来交互呢?数据库!对,就是它存储数据信息的.而数据库都能很方便的访问操作.把发送短信的后台服务定时去访问一个数据库,而另外任何要发送短信的地方也访问数据库,并插入一条要发送的短信到表里面,稍后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 ); 再添加一个服务类继承自ServiceBase,我们可以重写基类的OnStart、OnPause、OnStop、OnContinue等方法来实现我们需要的功能并设置指定一些属性.由于是定事发送短信的服务,自然少不了Windows记时器,在OnStart事件里我们写入服务日志,并初始化记时器.private System.Timers.Timer time;private static readonly string CurrentPath = Application.StartupPath + ;protected override void OnStart(string args)string path = CurrentPath + Logstart-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(); 实例化记时器类启动后,将在指定时间间隔触发Elapsed指定事件,如上GetSettings为读取我App.config文件里一个配置节点(值为30)的方法,所以上面将会每隔30秒调用TimeOut方法.而改方法就是我们发短信的具体操作.代码如下:private void TimeOut(object sender, EventArgs e)tryif (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 = rdrphone.ToString();string message = rdrmessage.ToString();string sendtime = rdrsendtime.ToString();System.Text.Encoding encoder = System.Text.Encoding.GetEncoding(GB2312);string url = string.Format(05/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), rdrsysid, rdradmin_inner_code, rdruser_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 + Logerror.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(); 上面我们使用try、catch访问数据库,并记录错误异常信息. 发送短信是使用发送一个Web请求发送出去的,要注意请求url字符串的编码类型,要与请求页面编码一致,不然会出现乱码.上面我们请求的是智网通集团短信(网址:/)的Web接口,通过访问他的网站来实现发短信,当然还要传递一些用户名、密码、手机号码和要发送的短信息等参数.他的收费平均大概为7分/条的
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025灵活劳动合同的未来展望
- 2025豪华轿车租赁协议
- 全球数字藏品市场2025年市场潜力与市场细分领域技术创新趋势研究报告
- (多篇可选)高二家长会发言稿格式范文
- 外科护理学疾病案例及治疗措施选择题试卷
- 2025企业购销原材料合同模板
- 车辆转让协议书(未过户)2篇
- 常用消毒剂使用的试题及答案
- 2023年执业药师考试题及答案
- 现场管理培训试题及答案
- 光伏电站建设安全总监岗位职责
- 报废汽车回收拆解企业技术规范
- 特种设备重大事故隐患判定准则试题及答案
- 三级安全教育试题及答案
- 脱硝培训试题一及答案
- 两人合伙贷款合同范本
- 人工智能全套课件下载
- 美容护肤产品合同协议
- 2025-2030中国高压造影剂注射管行业市场现状供需分析及投资评估规划分析研究报告
- 终止妊娠药品规范化管理
- 护理输入过期液体不良事件
评论
0/150
提交评论