简体   繁体   English

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

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

I have created a wcf service which is deployed via a managed windows service. 我创建了一个wcf服务,该服务是通过托管Windows服务部署的。 what the onstart() does is it creates and opens a tcp host for the wcf serice. onstart()的作用是创建并打开wcf服务的tcp主机。

everything works fine in windows 7 but when I try to install the service in windows server 2008 R2 the service starts and then stops with the error that sometimes services stop when there is nothing to do. 在Windows 7中一切正常,但是当我尝试在Windows Server 2008 R2中安装该服务时,该服务启动,然后停止,并出现错误,有时该服务在无事可做时会停止。 It is run under the network service account. 它在网络服务帐户下运行。

I cant find anything usefull in the windows logs. 我在Windows日志中找不到任何有用的东西。

I have tryed installing a dubug build and call debugger.launch() but its not working. 我尝试安装dubug版本并调用debugger.launch(),但无法正常工作。 I cant use remode debug because the the process does not stay open long enough for me to attach to it. 我不能使用重新调试模式,因为该过程无法保持足够长的打开时间,我无法对其进行附加。 I really dont know what to do. 我真的不知道该怎么办。 Here is my code: 这是我的代码:

    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);
    }

this code as is works perfectly fine in windows 7 but I cant get it to run on win 2008 R2 server. 这段代码在Windows 7中工作得很好,但是我无法在Win 2008 R2服务器上运行它。

Thanks in advance 提前致谢

Refactor your application as a console application , and then run it on the desired environment to be able to debug it easily. 将您的应用程序重构为控制台应用程序 ,然后在所需的环境中运行它,以便能够轻松对其进行调试。

I'm sure that the problem will reveal itself once you run your console replica under the same user account that is assigned to your windows service 我确信一旦以分配给Windows服务的同一用户帐户运行控制台副本,问题便会解决。

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

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