ASP.NET定时调用WebService 运行后台代码
效果:
通過在網站的Global.asax的Application_Start方法中
加入定時器 定時調用WebService
該WebService的一個方法 負責在后臺?
向數據庫的某個表加入數據
步驟:
1.通過VS 新建一個網站
2.加入Global.asax
3.加入WebService 編輯 并 加入引用
4.對Global.asax進行編輯
5.保存 運行 網站 查看效果
=============================
1.通過VS 新建一個網站
2.加入Global.asax?
-----------------
其默認內容如下:
<%@ Application Language="C#" %>
<script runat="server">
????
??? void Application_Start(object sender, EventArgs e)?
??? {
??????? // 在應用程序啟動時運行的代碼
??? }
????
??? void Application_End(object sender, EventArgs e)?
??? {
??????? //? 在應用程序關閉時運行的代碼
??? }
????????
??? void Application_Error(object sender, EventArgs e)?
??? {?
??????? // 在出現未處理的錯誤時運行的代碼
??? }
??? void Session_Start(object sender, EventArgs e)?
??? {
??????? // 在新會話啟動時運行的代碼
??? }
??? void Session_End(object sender, EventArgs e)?
??? {
??????? // 在會話結束時運行的代碼。?
??????? // 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為
??????? // InProc 時,才會引發 Session_End 事件。如果會話模式設置為 StateServer?
??????? // 或 SQLServer,則不會引發該事件。
??? }
???????
</script>
3.加入WebService 編輯 并 加入引用
4.對Global.asax進行如下編輯:
----------------------------
void Application_Start(object sender, EventArgs e)?
??? {
??????? // 在應用程序啟動時運行的代碼
??????? System.Timers.Timer myTimer = new System.Timers.Timer(60000);
??????? myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
??????? myTimer.Interval = 60000;
??????? myTimer.Enabled = true;
??? }
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
??? {
??????? localhost.WebService a = new localhost.WebService();
??????? string s = a.HelloWorld();
??? }
5.保存 運行 網站 查看效果
轉載于:https://www.cnblogs.com/top5/archive/2010/04/07/1706683.html
總結
以上是生活随笔為你收集整理的ASP.NET定时调用WebService 运行后台代码的全部內容,希望文章能夠幫你解決所遇到的問題。