Visual C# - 服务在Windows 7上运行,但不在Windows 10上运行

时间:2016-06-30 19:41:36

标签: c# .net visual-studio service startup

我有一个与另一个应用程序一起运行的自定义服务。该服务管理应用程序所依赖的数据库。该应用程序在Windows 7计算机上安装并运行完美,但在Windows 10上运行该应用程序时,该服务无法启动。在安装和运行应用程序时,我确保以管理员身份运行它们。

查看任务管理器,在服务启动并失败后,服务显示但状态为“已停止”。我已经尝试评论代码的功能正文,但服务的状态仍显示为“已停止”。

我一直坚持这个问题一段时间并且调试它并不容易。该程序正在使用Framework 3.5进行编译。任何帮助将不胜感激。

您可以在下面找到以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Chant.Datatest.Database;
using System.Configuration.Install;
using System.Security.Permissions;
using Chant.Common;
using System.IO;

namespace Chant.Datatest.Server
{
    [SecurityPermission(SecurityAction.LinkDemand)]
    public partial class DatatestDB : ServiceBase
    {
        TcpServerChannel serverChannel;
        CustomerDatabase m_database;
        ServerAdmin m_admin;
        AppSettings m_settings;
        System.Timers.Timer m_backupTimer;
        bool backupOS;
        int prevDay;

        public DatatestDB()
        {
            InitializeComponent();
            this.ServiceName = "chantDBsvc";
            this.CanShutdown = false;
            this.CanStop = true;
            this.AutoLog = true;
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                m_settings = new AppSettings();
                BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
                provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

                IDictionary properties = new Hashtable();
                properties.Add("port", m_settings.DBPort);
                properties.Add("secure", true);

                serverChannel = new TcpServerChannel(properties, provider);
                ChannelServices.RegisterChannel(serverChannel, true);

                m_database = new CustomerDatabase();
                m_database.EnableAutoBackup = m_settings.EnableAutoBackup;
                m_database.BackupCount = m_settings.BackupCount;
                m_database.SetFilePath(m_settings.GetOldDBPath(), m_settings.LogFilePath);
                m_database.LoadDatabase();

                RemotingServices.Marshal(m_database, "DatatestDB.rem");

                m_admin = new ServerAdmin(m_settings);
                m_admin.DatatestDatabase = m_database;

                RemotingServices.Marshal(m_admin, "DatatestAdmin.rem");

                backupOS = false;
                prevDay = DateTime.Now.Day;

                m_backupTimer = new System.Timers.Timer();
                m_backupTimer.Interval = 5000;
                m_backupTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_backupTimer_Elapsed);
                m_backupTimer.Start();
                this.EventLog.WriteEntry("Chant DataTEST Server Started.  Database loaded from: " + m_settings.DBPath + ", Listening on port: " + m_settings.DBPort);
            }
            catch (Exception ex)
            {
                this.EventLog.WriteEntry(ex.ToString());
            }
        }

        void m_backupTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            m_backupTimer.Stop();

            if (m_settings.EnableBackup)
            {
                bool timeReached = TimeReached(m_settings.BackupTime);
                bool doBackup = false;
                if (timeReached)
                {
                    switch (m_settings.BackupType)
                    {
                        case IntervalType.Daily:
                            m_database.CopyDatabase(m_settings.DBPath, m_settings.DBBackupPath);
                            break;
                        case IntervalType.Weekly:
                            switch (DateTime.Now.DayOfWeek)
                            {
                                case DayOfWeek.Sunday:
                                    if (m_settings.OnSun)
                                        doBackup = true;
                                    break;
                                case DayOfWeek.Monday:
                                    if (m_settings.OnMon)
                                        doBackup = true;
                                    break;
                                case DayOfWeek.Tuesday:
                                    if (m_settings.OnTue)
                                        doBackup = true;
                                    break;
                                case DayOfWeek.Wednesday:
                                    if (m_settings.OnWed)
                                        doBackup = true;
                                    break;
                                case DayOfWeek.Thursday:
                                    if (m_settings.OnThur)
                                        doBackup = true;
                                    break;
                                case DayOfWeek.Friday:
                                    if (m_settings.OnFri)
                                        doBackup = true;
                                    break;
                                case DayOfWeek.Saturday:
                                    if (m_settings.OnSat)
                                        doBackup = true;
                                    break;
                            }
                            break;
                        case IntervalType.Monthly:
                            if (DateTime.Now.Day == m_settings.MonthDay)
                                doBackup = true;
                            break;

                    }

                    if (doBackup)
                        m_database.CopyDatabase(m_settings.DBPath, m_settings.DBBackupPath);
                }
            }

            m_backupTimer.Start();
        }

        private bool TimeReached(DateTime timeIn)
        {
            bool reached = false;
            if ((timeIn.Hour >= DateTime.Now.Hour) &&
                (timeIn.Minute >= DateTime.Now.Minute) && (!backupOS))
            {
                reached = true;
                backupOS = true;
            }

            if (DateTime.Now.Day != prevDay)
            {
                backupOS = false;
            }
            prevDay = DateTime.Now.Day;

            return reached;
        }

        protected override void OnStop()
        {
            m_database.SaveDatabase(true, false, null, "");
            this.EventLog.WriteEntry("Chant DataTEST Server Stopped.  Database saved to: " + m_settings.DBPath);
        }
    }

    [RunInstaller(true)]
    public class WindowsServiceInstaller : Installer
    {
        /// <summary>
        /// Public Constructor for WindowsServiceInstaller.
        /// - Put all of your Initialization code here.
        /// </summary>
        public WindowsServiceInstaller()
        {
            ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            //# Service Account Information
            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            serviceProcessInstaller.Username = null;
            serviceProcessInstaller.Password = null;

            //# Service Information
            serviceInstaller.DisplayName = "Chant DataTEST Server";
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.Description = "Manages remote access to the Chant DataTEST customer database";

            //# This must be identical to the WindowsService.ServiceBase name
            //# set in the constructor of WindowsService.cs
            serviceInstaller.ServiceName = "chantDBsvc";

            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}

0 个答案:

没有答案