简体   繁体   中英

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. what the onstart() does is it creates and opens a tcp host for the wcf serice.

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. It is run under the network service account.

I cant find anything usefull in the windows logs.

I have tryed installing a dubug build and call debugger.launch() but its not working. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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