C#使用windows服務定時調用api接口


使用VS創建windows服務項目:

創建好項目  會出現一個設計界面 右鍵彈出對話框 選擇添加安裝程序

名字什么的自己可以改:

項目目錄:

 

打開項目中的ProjectInstaller.Designer.cs 修改windows服務名稱描述以及啟動方式等:

partial class ProjectInstaller
    {
        /// <summary>
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 組件設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        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 = "CallApiExeTask";

            //服務描述
            this.sInstaller.Description = "定時調用api接口,獲取任務后操作數據庫";

            // 設定服務的啟動方式
            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.spInstaller,
            this.sInstaller});

        }

        #endregion

        private System.ServiceProcess.ServiceProcessInstaller spInstaller;
        private System.ServiceProcess.ServiceInstaller sInstaller;
    }

 

打開Service1 寫入想要執行的操作等:

public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1;  //計時器
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //服務開啟執行代碼
            //定時調用接口

            timer1 = new System.Timers.Timer();
            timer1.Interval = 3000;  //設置計時器事件間隔執行時間
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
            timer1.Enabled = true;

            

        }
        /// <summary>
        /// 定時器 調用的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            var client = new HttpClient();
            client.BaseAddress = new Uri("http://192.168.10.239:9000/");//接口url
            string data = client.GetStringAsync("ZyTest").Result;//接口action
        }

        protected override void OnStop()
        {
            //服務結束執行代碼
            this.timer1.Enabled = false;
        }


        protected override void OnPause()
        {
            //服務暫停執行代碼
            base.OnPause();
        }
        protected override void OnContinue()
        {
            //服務恢復執行代碼
            base.OnContinue();
        }
        protected override void OnShutdown()
        {
            //系統即將關閉執行代碼
            base.OnShutdown();
        }
    }

 

Program.cs中可以設置主方法調用的service服務:

static class Program
    {
        /// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }

 

生成解決方案,以上就完成了windows服務的編寫

 

下面需要把服務安裝到服務器或者pc上:

 

首先在網上下載一個installutil.exe文件(百度直接搜索可以下載) 放到...\bin\Debug文件夾下:

  

 

用管理員身份打開命令提示符:

 

輸入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回車

如果你的程序是2.0的framework則需要輸入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727

 

輸入 InstallUtil.exe D:\work\windows服務\WinService4ExeApi\WinService4ExeApi\bin\Debug\WinService4ExeApi.exe 回車 完成安裝

說明: D:\work\windows服務\WinService4ExeApi\WinService4ExeApi\bin\Debug\WinService4ExeApi.exe表示項目生成的exe文件位置(也可以把debug文件夾單獨copy到其他地方重命名)

 

安裝完成后:打開服務就可以看到了

 

如果需要卸載此服務:打開cmd  直接輸入 sc delete  CallApiExeTask便可  CallApiExeTask為你的服務名稱

 

ps:  如果你的windows服務程序修改了 需要更新成新版本 不用卸載服務再安裝 只需先停掉該服務 然后把文件夾內的文件替換為新的文件 重新啟動該服務即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM