vs2015開發Windows服務

工做已經好久,時隔這麼長時間寫這篇文章是給本身之後作參考。也不至於之後長時間不寫Windows服務而忘記整個開發過程。windows服務開發,基礎的就不說了,直接上過程。數據庫

一、新建windows服務項目,我這裏選擇的是Framework4.0,沒有選擇高版本是爲了防止在服務在一些低版本系統上沒法正常運行。windows

二、添加Windows服務的安裝程序。ide

在默認Service1設計器界面空白處點擊右鍵->添加安裝程序,系統會自動新建一個帶有默認配置的安裝程序類,以下圖:this

新建完安裝程序後,須要給默認的serviceInstaller1和serviceProcessInstaller1作一些基本的屬性設置。以下圖:spa

以上工做完成,安裝程序配置完畢。設計

注意:若是不給服務添加安裝程序,後面是無法把服務安裝至windows系統裏的。3d

 三、添加應用程序配置文件(若是有須要的話)。日誌

若是項目有須要,一些應用程序的配置參數能夠設置在此文件裏(例如:數據庫鏈接字符串)。blog

 四、編寫windows服務主代碼事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.IO;

namespace OrganizClientSocketService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent);
            timer.Interval = 5000;//每5秒執行一次
            timer.Enabled = true;
        }

        //定時執行事件
        private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            //業務邏輯代碼
        }

        protected override void OnStart(string[] args)
        {
            this.WriteLog("搜才Organiz客戶端數據同步服務:【服務啓動】");
        }

        protected override void OnStop()
        {
            this.WriteLog("搜才Organiz客戶端數據同步服務:【服務中止】");
        }
        protected override void OnShutdown()
        {
            this.WriteLog("搜才Organiz客戶端數據同步服務:【計算機關閉】");
        }

        #region 記錄日誌
        /// <summary>
        /// 記錄日誌
        /// </summary>
        /// <param name="msg"></param>
        private void WriteLog(string msg)
        {

            //string path = @"C:\log.txt";

            //該日誌文件會存在windows服務程序目錄下
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
            FileInfo file = new FileInfo(path);
            if (!file.Exists)
            {
                FileStream fs;
                fs = File.Create(path);
                fs.Close();
            }

            using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(DateTime.Now.ToString() + "   " + msg);
                }
            }
        }
        #endregion
    }
}

五、編譯生成,安裝windows服務至Windows系統。

    完成開發後,對整各項目進行編譯生成。在windows服務開發文件夾「\bin\Debug」下,就是咱們須要安裝的服務,建議把裏面的全部文件拷貝至系統裏的某個目錄進行安裝。

  我是把整個個文件夾裏的文件拷貝到c:\WindowService文件夾下。而後打開目錄C:\Windows\Microsoft.NET\Framework64\v4.0.30319,拷貝里面的InstallUtil.exe文件至c:\WindowService文件夾下)。

  注意:個人系統是windows10,64位系統,個人服務也將安裝至64位系統下,因此我是進入C:\Windows\Microsoft.NET\Framework64\v4.0.30319目錄拷貝InstallUtil.exe文件。各位安裝的時候,根據你安裝的目標系統,來以爲是拷貝哪一個framework哪一個版本,具體是64位的仍是32位的也由你係統決定。

  作好以上工做後就能夠安裝了,打開cdm就可執行安裝了(必定要以管理員身份運行喲,要否則安裝時會報「Windows服務安裝異常:System.Security.SecurityException: 未找到源,但未能搜索某些或所有事件」)

  如下是安裝命令、啓動服務命令、中止服務命令、卸載服務命令:

    安裝命令:C:\WindowService\InstallUtil.exe C:\WindowService\OrganizClientSocketService.exe 

    啓動服務命令:net start 搜才Organiz客戶端數據同步服務

    關閉服務命令:net stop 搜才Organiz客戶端數據同步服務

    卸載服務命令:C:\WindowService\InstallUtil.exe -u C:\WindowService\OrganizClientSocketService.exe

相關文章
相關標籤/搜索