Topshelf服务无法作为服务启动:错误1053服务未响应。”

时间:2020-08-21 11:28:59

标签: c# .net service windows-services topshelf

我可以使用与服务运行时相同的帐户来调用可执行文件,从而直接从控制台窗口中运行该服务。但是,当我将其安装为服务并尝试运行时,它会显示以下消息:

enter image description here

在发布代码之前,这是我尝试过的操作:

以下是完整的异常消息:

事务处理安装已完成。 Topshelf.Hosts.StartHost错误: 0:服务无法启动。System.InvalidOperationException: 无法在计算机“。”上启动服务MyCabinet。 ---> System.ComponentModel.Win32Exception:服务未响应 及时启动或控制请求---内部结束 异常堆栈跟踪--- System.ServiceProcess.ServiceController.Start(String [] args)在 System.ServiceProcess.ServiceController.Start()位于 Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(字符串 serviceName,TimeSpan startTimeOut)位于 Topshelf.Hosts.StartHost.Run()

代码如下:

Program.cs below:

using SmartCabinet.Core.Domain.Entities;
using SmartCabinet.Infrastructure.Database;
using System;
using System.IO;
using Topshelf;

namespace SmartCabinet
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
                HostFactory.Run(serviceConfig =>                                   
                {
                    serviceConfig.Service<FilesProcessor>(s =>                                   
                    {
                        s.ConstructUsing(name => new FilesProcessor());
                        s.WhenStarted(execute => execute.Start());       //.BeforeStartingService(a => a.RequestAdditionalTime(TimeSpan.FromSeconds(120)));                         
                        s.WhenStopped(execute => execute.Stop());                          
                    });

                    //serviceConfig.SetServiceName("MyCabinet");
                    serviceConfig.SetDisplayName("Files Processor.");
                    serviceConfig.SetDescription("Windows service for files processing.");
                    serviceConfig.StartAutomatically();
                });                                                            
            }
            catch(Exception ex)
            {
                var error = new ErrorLog()
                {
                    ExceptionTitle = ex.Message,
                    ExceptionDescription = ex.InnerException?.Message,
                    CreatedDate = DateTime.Now
                };

                using (var context = new SmartCabinetDBContext())
                {
                    context.ErrorLogs.Add(error);
                    context.SaveChanges();
                }
            }
        }
    }
}

FilesProcessor.cs下面

namespace MyCabinet
{
    class FileProcessor
    {
        readonly System.Timers.Timer _timer;
        public FileProcessor()
        {
            ProcessAndImportData();

            _timer = new System.Timers.Timer(120000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) => ProcessAndImportData();

            //_timer.Elapsed += Timer_Elapsed;
            //_timer.Enabled = true;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
           ProcessAndImportData();
        }

        public void ProcessAndImportData()
        {
            Console.WriteLine("It is {0} and processing has started", DateTime.Now);
            // ... rest of the code

        }

        private void Download(string site, string itemPath, string file)
        {
            // .. some code
        }

        private void Upload(byte[] buffer, string pathToUpload, string site)
        {
           // .. some code
        }
        
        
        

        public bool Start() { _timer.Start(); return true; }
        public bool Stop() { _timer.Stop(); return true; }

        /* PREVIOUS UNUSED CODE:
        
        public void foreverWhile()
        {
            while (true)
            {
                // to do something forever
            }
        }
        
        public bool Start()
        {
            try
            {
                var myThread = new Thread(new ThreadStart(foreverWhile));
                myThread.IsBackground = true;  // This line will prevent thread from working after service stop.
                myThread.Start();
                return true;

                //_timer.Start();
                //return true;
            }
            catch(Exception ex)
            {
                var error = new ErrorLog()
                {
                    ExceptionTitle = ex.Message,
                    ExceptionDescription = ex.InnerException?.Message,
                    CreatedDate = DateTime.Now
                };

                using (var context = new SmartCabinetDBContext())
                {
                    context.ErrorLogs.Add(error);
                    context.SaveChanges();
                }
            }
            return true;
        }
        public bool Stop() { _timer.Stop(); return true; }END OF PREVIOUS UNUSED CODE */
    }
}

0 个答案:

没有答案