ASP.NET MVC 中應用Windows服務以及Webservice服務開發分布式定時器

來源:互聯網
上載者:User

標籤:

ASP.NET MVC 中應用Windows服務以及Webservice服務開發分布式定時器
一:閑談一下:
1.現在任務跟蹤管理系統已經開發快要結束了,抽一點時間來寫一下,想一想自己就有成就感啊!! 
2.關於任務跟蹤管理系統項目中遇到的Windows服務以及Webservice的綜合應用的問題。

大家好這是我第二次寫部落格 ,寫的不好請大家多多諒解, 希望大家可以多多指正。

二:我稍微的整理了一下關於這個分布式定時器需求:
1.根據任務跟蹤管理系統中的資料庫的AnswerSheet 表格中找到客戶編碼(CustomerID 為空白) ,電話號碼不為空白的記錄
2.第一種情況:根據不為空白的電話號碼(從內部的過河兵系統的資料庫Customer表中查詢資料當然過河兵系統中要進行維護Phone電話號碼)則調用Webservice API 查詢客戶的CustomerGUID/CustomerID 並將這個字串返回到任務跟蹤管理系統如果兩個不為空白則調用AnswerSheetEdit()方法更新對應的任務跟蹤管理系統的AnswerSheet中的CustomerGUID/CustomerID

3.第二種情況:根據不為空白的電話號碼則Webservice API 查詢不到客戶的CustomerGUID/CustomerID那麼需要在過河兵系統的Survey表格中插入AnswerSheetID 和SurveyID  
並進行更新過河兵對應的Customer表中的CustomerGUID/CustomerID 資料

4.每天夜裡23:30 定時更新任務跟蹤管理系統的資料以及添加刪除過河兵系統的資料

5.分布式定時器的作用是定時更新任務跟蹤管理系統和過河兵系統的使用者以及問卷的資料。

6.Windows 定時器的原始碼

        private static readonly SurveyHandle _surveyHandle = new SurveyHandle();
        private static readonly WebServerInterFaceHandle _webServerInterFaceHandle = new WebServerInterFaceHandle();
        public ServiceTime()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            //定義定時器
            Timer myTimer = new Timer(1000);
            myTimer.Elapsed += new ElapsedEventHandler(TaskTimeAction.ServiceTime);
            myTimer.Enabled = true;
            myTimer.AutoReset = true;
            using (StreamWriter sw = new StreamWriter("D:\\任務跟蹤管理定時器日誌.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "定時器服務啟動");
            }
        }
        protected override void OnStop()
        {
            using (StreamWriter sw = new StreamWriter("D:\\任務跟蹤管理定時器日誌.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
            }
        }

 /// <summary>
            /// 定時器委託任務 調用的方法
            /// </summary>
            /// <param name="source"></param>
            /// <param name="e"></param>
            public static void ServiceTime(object source, ElapsedEventArgs e)
            {
                SurveyController surveyController = new SurveyController();
                if (DateTime.Now.ToString("HH:mm:ss") == "23:30:00")
                {
                 
                    Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "夜裡23:30");
                    List<AnswerSheet> answerSheetList = _surveyHandle.GetAnswerGuidList();
                    SurveyAndAnswerSheet surveyAndAnswer = new SurveyAndAnswerSheet();
                    foreach (var items in answerSheetList)
                    {
                        #region 定時同步更新任務跟蹤管理系統以及過河兵系統的使用者以及問卷的資料
                        AnswerSheet answerSheet = _surveyHandle.GetAnswerSheet(items.ID);
                        Survey survey = _surveyHandle.GetSurvey(answerSheet.SurveyID);
                        //進行查詢AnswerIndex中的詳細的內容
                        //AnswerSheet  answerSheetModel= _surveyHandle.GetAnswerSheetDetail(items.ID);   
                        string model = _webServerInterFaceHandle.GetCustomerJson(items.CustomerID, items.CustomerPhone);
                        using (StreamWriter sw = new StreamWriter("D:\\任務跟蹤管理系統定時器日誌.txt", true))
                        {
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "答卷編號:" + items.ID + "客戶編碼:" + model);
                        }
                        if (model != null)
                        {
                            string[] getCustomerID = model.Split(‘|‘);
                            string Image = null;
                            if (answerSheet.AttachList != null)
                            {
                                foreach (var image in answerSheet.AttachList)
                                {
                                    Image += image.imgURL.Split(‘|‘).Last();
                                }
                            }
                            answerSheet.CustomerGUID = getCustomerID[0];
                            answerSheet.CustomerID = getCustomerID[1];
                            surveyAndAnswer.AnswerSheet = answerSheet;
                            surveyAndAnswer.Survey = survey;
                            surveyController.AnswerSheetEdit(surveyAndAnswer, "/Uploads/" + Image);
                        }
                        #endregion
                    }
                    using (StreamWriter sw = new StreamWriter("D:\\任務跟蹤管理系統定時器日誌.txt", true))
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + ":" + "結束時間");
                    }
                }
            }

        private SelectCusutomer.WebUseSevrices webServices = new SelectCusutomer.JieChuDianUseSevrices();//真實環境
        private  SelectCustomer2.WebUseSevrices _webServices = new SelectCustomer2.JieChuDianUseSevrices();//測試環境
三為什麼要寫Windows服務和Webservice 服務
1.通過產品經理和客戶的最終的確定,最後讓我將這個新的功能添加進去,所以我也就積極的去做了。
用了半天的時間將定時器開發完成,最終放在ASP.NET MVC 的Global.asax 的檔案中。
2.下一步我建立了一個項目Webservice API當我寫這個API 的時候老大讓我用Webservice而不是WCF!WCF 多好啊!!通過又是半天的學習開始寫Webservice API 的服務經過調試和最後發布到測試的伺服器上最終沒有問題了。
3.於是兩者開始進行對接通過在任務跟蹤管理系統中進行調試完全沒有問題對接也成功了,兩邊的資料庫中的資料也就行更新了。
4.通過最後我將任務跟蹤管理系統發布到IIS 7.0上去到了指定的時間資料沒有更新找到了通過IIS閑置逾時的時間為1440分鐘以及修改應用程式集區的配置搞了一天最終還是沒有達到兩邊的系統的資料同步,
最終還是應用程式集區回收了記憶體資源以及上網查了一下關於IIS具有的不穩定性。
5.最後我決定用Windows 服務進行開發定時器原因穩定性以及效能都非常好。
四:關於Windows服務的建立以及調用Webservice服務
五:通過修改Windows服務下面的
App.config:來進行修改資料庫的配置的檔案串連資料庫字串。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ContactPoint" value="server=;database=TaskTrackData;uid=sa;password=sa;multipleactiveresultsets=True" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>
六:安裝服務通過命令
CMD 以管理員身份開啟然後執行就可以了。
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe D:\WindowsService\WindowsService.exe  
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u D:\WindowsService\WindowsService.exe  

ASP.NET MVC 中應用Windows服務以及Webservice服務開發分布式定時器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.