zoukankan      html  css  js  c++  java
  • 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服务开发文件夹“inDebug”下,就是我们需要安装的服务,建议把里面的所有文件拷贝至系统里的某个目录进行安装。

      我是把整个个文件夹里的文件拷贝到c:WindowService文件夹下。然后打开目录C:WindowsMicrosoft.NETFramework64v4.0.30319,拷贝里面的InstallUtil.exe文件至c:WindowService文件夹下)。

      注意:我的系统是windows10,64位系统,我的服务也将安装至64位系统下,所以我是进入C:WindowsMicrosoft.NETFramework64v4.0.30319目录拷贝InstallUtil.exe文件。各位安装的时候,根据你安装的目标系统,来觉得是拷贝哪个framework哪个版本,具体是64位的还是32位的也由你系统决定。

      做好以上工作后就可以安装了,打开cdm就可执行安装了(一定要以管理员身份运行哟,要不然安装时会报“Windows服务安装异常:System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件”)。

      以下是安装命令、启动服务命令、停止服务命令、卸载服务命令:

        安装命令:C:WindowServiceInstallUtil.exe C:WindowServiceOrganizClientSocketService.exe 

        启动服务命令:net start 搜才Organiz客户端数据同步服务

        关闭服务命令:net stop 搜才Organiz客户端数据同步服务

        卸载服务命令:C:WindowServiceInstallUtil.exe -u C:WindowServiceOrganizClientSocketService.exe  

      注意:

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

    个人能力有限,本文内容仅供学习、探讨,欢迎指正、交流。

     

      参考资料:

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

      

  • 相关阅读:
    How to bind to data when the DataContext is not inherited【项目】
    访问ControlTemplate内部的元素
    TemplateBinding vs TemplatedParent【PluraSight】
    UserControl和CustomControl基础【PluraSight】
    如何让label和textblock分成两行
    Labeled ContentControl & LabeledControl【项目】
    Label & TextBlock
    How to use SourceGear DiffMerge in SourceSafe, TFS, and SVN【项目】
    转载 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(一&二)——初识WiX
    Unity中的C#规则
  • 原文地址:https://www.cnblogs.com/sagecheng/p/7397310.html
Copyright © 2011-2022 走看看