VS2015 項目中 添加windows服務


1. 在項目中添加winows服務

今天剛剛為自己的項目添加了windows服務,以服務的形式運行后台系統,為前端提供接口服務,下面說一下具體怎么為vs項目添加windows服務

  

2. 添加Windows服務安裝程序

在上圖空白處點擊右鍵,如下圖所示

VS2015會自動新建一個帶有默認配置的安裝程序類,如下圖:

i

3. 給默認的serviceInstaller1和serviceProcessInstaller1的屬性進行置

如下圖所示:

  

 

4. 設置window是服務代碼:

 

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 = 10000;//每5秒執行一次
            timer.Enabled = true;
        }
 
        //定時執行事件
        private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            //業務邏輯代碼
        }
 
        protected override void OnStart(string[] args)
        {
            this.WriteLog("【服務啟動】");
        }
 
        protected override void OnStop()
        {
            this.WriteLog("服務停止】");
        }
        protected override void OnShutdown()
        {
            this.WriteLog("【計算機關閉】");
        }
 
        #region 記錄日志
        /// <summary>
        /// 記錄日志
        /// </summary>
        /// <param name="msg"></param>
        private void WriteLog(string msg)
        {
            //該日志文件會存在windows服務程序目錄下
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt"; //string path = @"C:\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
    }
}

 

 

5. 編譯生成,安裝服務到電腦

     完成開發后,對整各項目進行編譯生成。在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  

  注意:

 

版權所有,文章來源:http://www.cnblogs.com/sagecheng/p/7397310.html 

個人能力有限,本文內容僅供學習、探討,歡迎指正、交流。

 

  參考資料:

http://www.cnblogs.com/sagecheng/articles/7397293.html

  

 


免責聲明!

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



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