Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows service (hosting WCF service) stops immediately on start up

My Question: I cannot navigate to the base address once the service is installed because the service won't remain running (stops immediately). Is there anything I need to do on the server or my machine to make the baseAddress valid?

Background: I'm trying to learn how to use WCF services hosted in Windows Services. I have read several tutorials on how to accomplish this and it seems very straight forward. I've looked at this MSDN article and built it step-by-step. I can install the service on my machine and on a server, but when I start the service, it stops immediately. I then found this tutorial, which is essentially the same thing, but it contains some clients that consume the WCF service. I downloaded the source code, compiled, installed, but when I started the service, it stopped immediately.

Searching SO, I found a possible solution that said to define the baseAddress when instantiating the ServiceHost, but that didnt help either.

My serviceHost is defined as:

serviceHost = new ServiceHost( typeof( CalculatorService ), 
                 new Uri( "http://localhost:8000/ServiceModelSamples/service" ) );

My service name, base address, and endpoint:

<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
        <host>
            <baseAddresses>
                <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
            </baseAddresses>
        </host>
        <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>

I've verified the namespaces are identical. It's just getting frustrating that the tutorials seem to assume that the Windows service will start as long as all the stated steps are followed. I'm missing something and it's probably right in front of me. Please help!

like image 627
norepro Avatar asked Dec 21 '22 19:12

norepro


2 Answers

When your windows service stops immediately, there is an exception raised in the OnStart method. Please try to catch and log that exception.

like image 52
Jan Avatar answered Feb 04 '23 07:02

Jan


Late to this, but I find this line as the first line in OnStart to be immensely helpful in debugging services:

System.Diagnostics.Debugger.Launch(); 

You can then select a running session of Visual Studio with your project already loaded (note: you might need to have that running as admin) and start stepping through your code.

like image 32
Matt Burland Avatar answered Feb 04 '23 07:02

Matt Burland