繁体   English   中英

Windows服务在Windows 7上启动,但在Windows Server 2008 R2上无法启动

[英]Windows service starts on Windows 7 but fails to start on Windows Server 2008 R2

我创建了一个wcf服务,该服务是通过托管Windows服务部署的。 onstart()的作用是创建并打开wcf服务的tcp主机。

在Windows 7中一切正常,但是当我尝试在Windows Server 2008 R2中安装该服务时,该服务启动,然后停止,并出现错误,有时该服务在无事可做时会停止。 它在网络服务帐户下运行。

我在Windows日志中找不到任何有用的东西。

我尝试安装dubug版本并调用debugger.launch(),但无法正常工作。 我不能使用重新调试模式,因为该过程无法保持足够长的打开时间,我无法对其进行附加。 我真的不知道该怎么办。 这是我的代码:

    protected override void OnStart(string[] args)
    {
        System.Diagnostics.Debugger.Launch();

        try
        {

            //if (!System.Diagnostics.EventLog.SourceExists("i2s CU Service (eng)"))
            //{
            //    System.Diagnostics.EventLog.CreateEventSource("i2s CU Service", "Application");
            //}
            System.Diagnostics.EventLog.CreateEventSource("i2s CU Service", "Application");
        }
        catch (Exception)
        {
            //throw;
        }
        eventLog1.Source = "i2s CU Service";
        eventLog1.Log = "Application";

        if (serviceHost != null)
        {
            serviceHost.Close();
        }

        System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader();
        Uri tcpUri = null;
        try
        {
            tcpUri = new Uri((string)reader.GetValue("Uri", typeof(string))); //net.tcp://localhost:8008/i2sServer

            serviceHost = new ServiceHost(typeof(ConcurrentUsers), tcpUri);

            // Create a binding that uses TCP and set the security mode to none.
            NetTcpBinding b = new NetTcpBinding();
            b.Security.Mode = SecurityMode.None;

            // Add an endpoint to the service.
            serviceHost.AddServiceEndpoint(typeof(IConcurrentUsers), b, "");

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();
        }
        catch (Exception ex)
        {
            eventLog1.WriteEntry(ex.Message, EventLogEntryType.Error);
        }


        if (serviceHost.State == CommunicationState.Opened)
        {
            eventLog1.WriteEntry("Service started.", EventLogEntryType.Information);
        }
    }

    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
        eventLog1.WriteEntry("Service stopped.", EventLogEntryType.Information);
    }

这段代码在Windows 7中工作得很好,但是我无法在Win 2008 R2服务器上运行它。

提前致谢

将您的应用程序重构为控制台应用程序 ,然后在所需的环境中运行它,以便能够轻松对其进行调试。

我确信一旦以分配给Windows服务的同一用户帐户运行控制台副本,问题便会解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM