ASP.NET 自动运行程序

              项目发布以后,假如想要一个可以自动执行的程序在后台运行,比如每隔1分钟去扫描数据库发送邮件,这样的需求经常见,在ASP.NET中我们可以采取两种方法实现。

一、在项目的Global.asax文件中添加自动执行程序。

        我们可以在Application_Start方法中添加自动运行的代码,当Web程序一旦发布以后,改代码就可以自动运行了。实例代码如下:

<%@ Application Language="C#" %>
<script RunAt="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        // 加载网站设置
        OThinker.H3.WorkSheet.SheetUtility.EnsureSettings(this.Application);
        //定义定时器
        System.Timers.Timer myTimer = new System.Timers.Timer(60000); //设置时间为1分钟
        myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
        myTimer.Enabled = true;
        myTimer.AutoReset = true;
        myTimer.Start();
    }

    //每隔1分钟读取H3数据库中待发送的短信列表 发送短信 更新状态值
    static void myTimer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["H3"].ToString();
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(strconn);
            System.Data.DataSet ds = new System.Data.DataSet();
            try
            {
                connection.Open();
                string sqlStr = "SELECT ID,Number,Message FROM SMS_SendMessage WHERE State=0";
                System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(sqlStr, connection);
                System.Data.SqlClient.SqlDataAdapter command = new System.Data.SqlClient.SqlDataAdapter(sqlCommand);
                command.Fill(ds);
                if (ds != null && ds.Tables[0].Columns.Count > 0)
                {
                    foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                    {
                        //设置短信提醒URL
                        string sendMessageUrl = string.Format("http://office.zhongnangroup.cn:87/pmoa/commondoc/commonphoneinterface.jsp?phone={0}&content={1}&type={2}",
                            dr["Number"].ToString(), HttpUtility.UrlEncode(dr["Message"].ToString(), Encoding.GetEncoding("GB2312")), "bpm");
                        System.Net.WebRequest request = System.Net.WebRequest.Create(sendMessageUrl);//发送request
                        System.Net.WebResponse response = request.GetResponse();//获取返回结果
                        System.IO.Stream resStream = response.GetResponseStream();
                        System.IO.StreamReader sr = new System.IO.StreamReader(resStream, Encoding.Default);
                        StringBuilder sb = new StringBuilder();
                        string r = string.Empty;
                        while ((r = sr.ReadLine()) != null)
                        {
                            sb.Append(r);
                        }
                        r = sb.ToString();
                        resStream.Close();
                        sr.Close();
                        if (r == "true")//发送短信成功 更新状态值
                        {
                            string updateSql = "UPDATE SMS_SendMessage SET State=1,SendTime=getdate() WHERE ID='" + dr["ID"].ToString() + "'";
                            System.Data.SqlClient.SqlCommand sqlUpdateCommand = new System.Data.SqlClient.SqlCommand(updateSql, connection);
                            sqlUpdateCommand.ExecuteNonQuery();
                        }
                    }
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
        catch (Exception ee)
        {
            throw ee;
        }
    }

    void Application_End(object sender, EventArgs e)
    {
        //Code that runs on application shutdown
        //解决IIS应用程序池自动回收的问题
        System.Threading.Thread.Sleep(1000);
        //激发Application_Start
        string url = "http://www.baidu.com/";
        System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
        System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
    }
       
</script>

这种方法的缺点很明显,就是程序运行到一定时间后会自动停止,必须在Application_End方法中激活一下。

二、开发一个windows服务,后台驻留。我觉得这种方法是最灵活也是最好的。

1、在项目中添加一个windows服务项目。

2、打开设计器然后切换到代码界面,实例代码如下:

namespace MessageService
{
    public partial class SendService : ServiceBase
    {
        public SendService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            WriteLog("Service Started");
            //定义定时器
            System.Timers.Timer myTimer = new System.Timers.Timer(60000); //设置时间间隔为1分钟
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
            myTimer.Enabled = true;
            myTimer.AutoReset = true;
            myTimer.Start();
        }

        //每隔1分钟读取H3数据库中待发送的短信列表 发送短信 更新状态值
        protected void myTimer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                string strconn = "Data Source=10.10.19.230;Initial Catalog=H3;Persist Security Info=True;User ID=sa;password=password123456;";
                SqlConnection connection = new SqlConnection(strconn);
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    string sqlStr = "SELECT ID,Number,Message FROM SMS_SendMessage WHERE State=0";
                    SqlCommand sqlCommand = new SqlCommand(sqlStr, connection);
                    SqlDataAdapter command = new SqlDataAdapter(sqlCommand);
                    command.Fill(ds);
                    if (ds != null && ds.Tables[0].Columns.Count > 0)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            //设置短信提醒URL
                            string sendMessageUrl = string.Format("http://office.zhongnangroup.cn:87/pmoa/commondoc/commonphoneinterface.jsp?phone={0}&content={1}&type={2}",
                                dr["Number"].ToString(), HttpUtility.UrlEncode(dr["Message"].ToString(), Encoding.GetEncoding("GB2312")), "bpm");
                            WebRequest request = WebRequest.Create(sendMessageUrl);//发送request
                            request.Timeout = 10000;//设置超时时间
                            WebResponse response = request.GetResponse();//获取返回结果
                            Stream resStream = response.GetResponseStream();
                            StreamReader sr = new StreamReader(resStream, Encoding.Default);
                            StringBuilder sb = new StringBuilder();
                            string r = string.Empty;
                            while ((r = sr.ReadLine()) != null)
                            {
                                sb.Append(r);
                            }
                            r = sb.ToString();
                            resStream.Close();
                            sr.Close();
                            if (r == "true")//发送短信成功 更新状态值
                            {
                                string updateSql = "UPDATE SMS_SendMessage SET State=1,SendTime=getdate() WHERE ID='" + dr["ID"].ToString() + "'";
                                SqlCommand sqlUpdateCommand = new SqlCommand(updateSql, connection);
                                sqlUpdateCommand.ExecuteNonQuery();
                                WriteLog("Send Successfully. MessageID: " + dr["ID"].ToString());
                            }
                            else
                            {
                                WriteLog("Send Failed. MessageID: " + dr["ID"].ToString());
                            }
                        }
                    }
                }
                catch (SqlException ex)
                {
                    WriteLog(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            catch (Exception ee)
            {
                WriteLog(ee.Message);
            }
        }

        protected override void OnStop()
        {
            WriteLog("Service Stop");
        }

        private void WriteLog(string content)
        {
            FileStream fs = new FileStream(@"c:\MessageService.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            StringBuilder sb = new StringBuilder();
            sb.Append("MessageService: ");
            sb.Append(content);
            sb.Append(" " + DateTime.Now.ToString() + "\n");
            m_streamWriter.WriteLine(sb.ToString());
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }
    }
}

 3、打开设计器视图,右键单击,添加安装程序,然后设置 服务的启动类型和名称等等属性。

 4、生成服务程序,拷贝服务exe文件,然后使用 安装工具 InstallUtil.exe工具安装服务。命令行如下:

        InstallUtil  f:\MessageService.exe   安装服务

       InstallUtil  -u  f:\MessageService    卸载服务

       

 

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值