码迷,mamicode.com
首页 > Windows程序 > 详细

C#创建Windows服务与安装-图解

时间:2016-02-15 18:16:54      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

1.创建windows服务项目

技术分享

2.右键点击Service1.cs,查看代码, 用于编写操作逻辑代码

技术分享

3.代码中OnStart用于执行服务事件

  public partial class Service1 : ServiceBase
    {
        string logFilePath = "";
        LogHelper logHelper;
        WendyWuBll bll = new WendyWuBll();
        public Service1()
        {
            logFilePath = ConfigurationManager.AppSettings["syncFeefoDataLog"];
            logHelper = new LogHelper(logFilePath);
            InitializeComponent();
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);        
        }
        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                logHelper.WriteLine("来自“MonitorOnServer”的全局异常。" + ex.Message + "详细信息如下:"
                                    + Environment.NewLine + "[InnerException]" + ex.InnerException
                                    + Environment.NewLine + "[Source]" + ex.Source
                                    + Environment.NewLine + "[TargetSite]" + ex.TargetSite
                                    + Environment.NewLine + "[StackTrace]" + ex.StackTrace);
            }
            catch { }
        }

        private delegate void NewTaskDelegate();
        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("同步feefo数据服务启动");  //在系统事件查看器里的应用程序事件里来源的描述
            logHelper.WriteLine("服务启动");       //自定义日志
            logHelper.WriteLine("服务版本1.1");
            NewTaskDelegate task = bll.IntData;
            //使用BeginInvoke方法异步调用newTask方法,并传入2000作为newTask的参数
            IAsyncResult asyncResult = task.BeginInvoke( null, null);
            System.Timers.Timer t = new System.Timers.Timer();
            t.Interval = 1000;
            t.Elapsed+=new System.Timers.ElapsedEventHandler(ChkSrv);//到达时间时候执行事件
            t.AutoReset = true;//设置执行一次(false)还是一直执行(true)
            t.Enabled = true;//是否执行system.Timers.Timer.ElapseEventHander事件
            bll.SetServiceStatus("WendyWuAutoService", "Running");
        }

        private void ChkSrv(object sender, System.Timers.ElapsedEventArgs e)
        {
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            int intSecond = e.SignalTime.Second;
            if ((intMinute == 00 && intSecond == 00) || (intMinute == 30 && intSecond == 00))//定时设置,判断每天八点执行
            {
               
                try 
                {
                    logHelper.WriteLine("开始从feefo同步数据");
                    System.Timers.Timer tt = (System.Timers.Timer)sender;
                    bll.SyncDataFromFeefo();
                    logHelper.WriteLine("从feefo同步数据完成");
                }
                catch(Exception ex)
                {
                    logHelper.WriteLine(ex.Message);
                }
            }
            else if ((intMinute == 15 && intSecond == 00) || (intMinute == 25 && intSecond == 00) || (intMinute == 35 && intSecond == 00) || (intMinute == 45 && intSecond == 00) || (intMinute == 55 && intSecond == 00))
            {
                try
                {
                    logHelper.WriteLine("自动邮件发送服务启动" + DateTime.Now.ToString());
                    bll.SendEmail();
                    logHelper.WriteLine("邮件发送完成" + DateTime.Now.ToString());
                }
                catch(Exception ex)
                {
                    logHelper.WriteLine(ex.Message);
                }
            }
        }

        protected override void OnStop()
        {
            logHelper.WriteLine("服务停止");
            EventLog.WriteEntry("同步feefo数据服务停止");
            bll.SetServiceStatus("WendyWuAutoService", "Stopped");
        }

    }

 

 

安装服务配置

1.打开Service1.cs视图界面

技术分享

2.在视图内右键-->添加安装程序

技术分享

3.

项目中添加了ProjectInstaller.cs文件,该文件中视图自动会添加俩个组件

serviceProcessInstaller1

serviceInstaller1

技术分享

4.选中serviceProcessInstaller1组件,查看属性,设置account为LocalSystem

技术分享

5. 选中serviceInstaller1组件,查看属性

  设置ServiceName的值, 该值表示在系统服务中的名称

  设置StartType, 如果为Manual则手动启动,默认停止,如果为Automatic为自动启动

  设置Description,添加服务描述

技术分享

安装服务

1.点击 开始,运行中输入cmd,获取命令提示符

win7需要已管理员的身份启动,否则无法安装

技术分享

2.

输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车

切 换当前目录,此处需要注意的是,在C:\Windows\Microsoft.NET\Framework目录下有很多类似版本,具体去哪个目录要看项目 的运行环境,例 如果是.net framework2.0则需要输入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727

技术分享

3.

输入 InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe 回车

说明:E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe表示项目生成的exe文件位置

技术分享

4.打开服务,就可以看到已经安装的服务了

技术分享

 

卸载服务

  1.  

    卸载很简单,打开cmd, 直接输入 sc delete WinServiceTest便可

C#创建Windows服务与安装-图解

标签:

原文地址:http://www.cnblogs.com/yzxblog/p/5190908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!