Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual C#-服务在Windows 7上运行,但不在Windows 10上运行_C#_.net_Visual Studio_Service_Startup - Fatal编程技术网

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

Visual C#-服务在Windows 7上运行,但不在Windows 10上运行,c#,.net,visual-studio,service,startup,C#,.net,Visual Studio,Service,Startup,我有一个与另一个应用程序一起运行的自定义服务。该服务管理应用程序所依赖的数据库。该应用程序可以在Windows 7计算机上完美安装和运行,但在Windows 10上运行该应用程序时,服务不会启动。在安装和运行应用程序时,我确保以管理员身份运行它们 查看任务管理器,在服务启动并失败后,服务显示,但状态为“已停止”。我已经试着注释代码的功能主体,但是服务的状态仍然显示为“stopped” 我已经被这个问题困扰了一段时间,调试它并不容易。该程序正在使用Framework 3.5进行编译。任何帮助都将不

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

查看任务管理器,在服务启动并失败后,服务显示,但状态为“已停止”。我已经试着注释代码的功能主体,但是服务的状态仍然显示为“stopped”

我已经被这个问题困扰了一段时间,调试它并不容易。该程序正在使用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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统诊断;
使用System.ServiceProcess;
使用系统文本;
使用系统集合;
使用System.Runtime.Remoting;
使用System.Runtime.Remoting.Channels;
使用System.Runtime.Remoting.Channels.Tcp;
使用Chant.Datatest.Database;
使用System.Configuration.Install;
使用System.Security.Permissions;
使用圣歌。常见;
使用System.IO;
命名空间Chant.Datatest.Server
{
[SecurityPermission(SecurityAction.LinkDemand)]
公共部分类DatatestDB:ServiceBase
{
TcpServerChannel服务器通道;
客户数据库m_数据库;
服务器管理员;
应用设置MU设置;
System.Timers.Timer m_备份器;
布尔巴库普斯;
国际日;
公共数据测试数据库()
{
初始化组件();
this.ServiceName=“chantDBsvc”;
this.CanShutdown=false;
this.CanStop=true;
this.AutoLog=true;
}
启动时受保护的覆盖无效(字符串[]args)
{
尝试
{
m_settings=新的应用设置();
BinaryServerFormatterSinkProvider=新的BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel=System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
IDictionary属性=新哈希表();
添加(“端口”,m_settings.DBPort);
属性。添加(“安全”,true);
serverChannel=新的TcpServerChannel(属性、提供程序);
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_数据库,“DatatestDB.rem”);
m_admin=新服务器管理员(m_设置);
m_admin.DatatestDatabase=m_数据库;
RemotingServices.Marshal(m_admin,“DatatestAdmin.rem”);
backupOS=false;
prevDay=DateTime.Now.Day;
m_backupTimer=新系统.Timers.Timer();
m_backupmer.Interval=5000;
m_backupmer.appeased+=新系统.Timers.ElapsedEventHandler(m_backupmer_appeased);
m_backupmer.Start();
this.EventLog.WriteEntry(“Chant DataTEST服务器已启动。数据库加载自:“+m_settings.DBPath+”,侦听端口:“+m_settings.DBPort”);
}
捕获(例外情况除外)
{
this.EventLog.WriteEntry(例如ToString());
}
}
无效m_备份程序(对象发送器,System.Timers.ElapsedEventArgs e)
{
m_backupmer.Stop();
如果(m_设置。启用备份)
{
bool timereach=timereach(m_设置.备份时间);
bool-doBackup=false;
如果(到达时间)
{
开关(m_设置。备份类型)
{
案例间隔类型。每日:
CopyDatabase(m_settings.DBPath、m_settings.DBBackupPath);
打破
案例间隔类型。每周:
开关(DateTime.Now.DayOfWeek)
{
星期日,星期日:
if(m_settings.OnSun)
doBackup=true;
打破
案件星期一星期一:
if(m_settings.OnMon)
doBackup=true;
打破
案件星期二:
if(m_settings.OnTue)
doBackup=true;
打破
星期三,星期三:
如果(m_settings.OnWed)
doBackup=true;
打破
星期四,星期四:
如果(m_settings.OnThur)
doBackup=true;
打破
案件星期五星期五:
if(m_settings.OnFri)
doBackup=true;
打破
星期六,星期六:
if(m_settings.OnSat)
多巴哥