Alaa Jabre
Alaa Jabre

Reputation: 1883

Windows service stopped without any errors

I created a windows service to host some WCF services,
but when I start it, it stop with a message: enter image description here

I checked windows log viewer and there isn't any errors

and I tested everything on a console application before and it's working.

My code is:

ServiceHost host1;
ServiceHost host2;
ServiceHost host3;

public ServicesHost()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    if (host1 != null)            
        host1.Close();
    if (host2 != null)
        host2.Close();
    if (host3 != null)
        host3.Close();            
    host1 = new ServiceHost(typeof(Service1));
    host1.Open();    
    host2 = new ServiceHost(typeof(Service2));
    host2.Open();    
    host3 = new ServiceHost(typeof(Service3));
    host3.Open();                       
}

protected override void OnStop()
{
    host1.Close();
    host1 = null;
    host2.Close();
    host2 = null;
    host3.Close();
    host3 = null;
}        

app.config:

<?xml version="1.0" encoding="utf-8" ?>

  <service behaviorConfiguration="MyServiceBehavior"
           name="Service2">
    <endpoint address=""
              binding="basicHttpBinding"
              contract="IService2">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Service2" />
      </baseAddresses>
    </host>
  </service>

  <service behaviorConfiguration="MyServiceBehavior"
           name="Service3">
    <endpoint address=""
              binding="basicHttpBinding"
              contract="IService3">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8733/Service3" />
      </baseAddresses>
    </host>
  </service>

</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Edit: I have installer:

[RunInstaller(true)]
public partial class ServiceInstaller : System.Configuration.Install.Installer
{
    private System.ServiceProcess.ServiceProcessInstaller process;
    private System.ServiceProcess.ServiceInstaller service;

    public ServiceInstaller()
    {            
        process = new System.ServiceProcess.ServiceProcessInstaller();
        process.Account = System.ServiceProcess.ServiceAccount.NetworkService;
        service = new System.ServiceProcess.ServiceInstaller();
        service.ServiceName = "WCFHostService";
        service.DisplayName = "WCFHostService";
        service.Description = "WCF Service Hosted";
        service.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
        Installers.Add(process);
        Installers.Add(service);
    }
}

Upvotes: 1

Views: 2501

Answers (2)

Felice Pollano
Felice Pollano

Reputation: 33242

Well, you are not logging at all. A window service should have a good logging system to survive in the production/mainteinance phase. Add a loggin system, put a try catch in the start, catch and log the exception in a file, this will hel you for the problem you are facing now, but help every day in your project lifetime.

Upvotes: 0

Jeroen van Langen
Jeroen van Langen

Reputation: 22038

When a console/desktop application runs but not as service, it's mostly a userright problem. This applies on like, using COM/DCOM or the use of files, because the current path for a service is the windows\system32.

Try to wrap the OnStart with try/catch and write the exception to the EventLog -> http://support.microsoft.com/kb/307024


Did you create any installers for the service? How to: Add Installers to Your Service Application http://msdn.microsoft.com/en-us/library/ddhy0byf.aspx

Upvotes: 1

Related Questions