.Net (C#) Service Fails To Start

K

Ken Allen

I have a number of services developed in C# (.Net), and they have been
working fine for the most part. Recently someone reported that ipon
occassion (originally rarely, but more frequently on some systems of
late), there is an entry in the Event Log immediately after a boot
indicating that the service failed to start after 30,000 milliseconds.
Usually it starts fine when attempting manually after the boot.

I instrumented the code to write entries to the Event Log during its
main() execution as well as during the OnStart() execution -- but when
it fails, there are no entries in the Event Log from the service code!

Attempting to launch the service as a console application always seems
to succeed. Interestingly, launching as a console application is about
10 times faster than starting the service! In no case that the service
loaded properly did the time pass the 500 milliseond duration!

I checked and there are no dependencies marked for the service. Is it
possible that .Net is not available at that time and the service cannot
execute?

How can I define any dependencies at compile time? The ServiceInstaller
permits me to define a list of services to depend on, but there does not
seem to be any dependency on other objects. Nor does there appear to be
any .Net service on which I could wait.

Does anyone have any ideas on why my service may be failing to load, but
only at boot time?

Where does the /LogToConsole=false switch setting come from on my
installed services? How do I change this to true, and what effect would
that have on my service?

-ken
 
J

Jared Parsons [MSFT]

One technique you could try if all else fails is to insert a try/catch block
around all of the code in your OnStart() method and show any errors in a
message box. Note this will require your service to have the "Interact With
Desktop" permission. Alternately, you could write the message to a known
file on disk.
 
K

Ken Allen

But the problem is that none of the code in the main() routine, which
must execute before the OnStart(), is producing output in this case
either. It is just as if the program was not able to launch -- but it
does launch since it appears in the list of services once the system
permits a logon!

-ken
 
W

Willy Denoyette [MVP]

Ken Allen said:
But the problem is that none of the code in the main() routine, which must
execute before the OnStart(), is producing output in this case either. It
is just as if the program was not able to launch -- but it does launch
since it appears in the list of services once the system permits a logon!

-ken

What exactly are you doing in your OnStart? Are you Impersonating or are you
accessing network resources?

Willy.
 
K

Ken Allen

In this service, the only thing that OnStart() does is create and start
a thread; the thread creates a named pipe and then processes the
information sent over the pipe until the thread exits. I have added some
debug code to create Event Log entries, but they, along with the debug
Event Log entries created in the main() routine, only appear if the
service starts. In the case where I get this error, which I can only
reproduce at boot time, there are no Event Log entries produced other
than the one from the service manager reporting that my service failed
to respond within 30,000 milliseconds.

Again, all of the .Net services I wrote launch fine most of the time,
but on upon occasion they exhibit this error on the boot of the system.
I cannot detect anything different, as not all boots on the same system
exhibit the same error! And the services all seem to start manually once
the system is booted.

-ken
 
A

Alan Pretre

Ken Allen said:
Again, all of the .Net services I wrote launch fine most of the time, but
on upon occasion they exhibit this error on the boot of the system. I
cannot detect anything different, as not all boots on the same system
exhibit the same error! And the services all seem to start manually once
the system is booted.

Make sure that you have your service dependencies set correctly so that all
required services are running before yours...

-- Alan
 
A

Alan Pretre

Ken Allen said:
I checked and there are no dependencies marked for the service. Is it
possible that .Net is not available at that time and the service cannot
execute?

Sorry, I did not read your post carefully enough before my last post.

There are no services for .NET itself. Try adding RPC as a dependency for
your service. For this you enter RPCSS in your dependency list. If built
and (re)installed correctly, you should see "Remote Procedure Call (RPC)" in
the Dependencies tab for the service in the SCM.

-- Alan
 
W

Willy Denoyette [MVP]

Ken Allen said:
In this service, the only thing that OnStart() does is create and start a
thread; the thread creates a named pipe and then processes the information
sent over the pipe until the thread exits. I have added some debug code to
create Event Log entries, but they, along with the debug Event Log entries
created in the main() routine, only appear if the service starts. In the
case where I get this error, which I can only reproduce at boot time,
there are no Event Log entries produced other than the one from the
service manager reporting that my service failed to respond within 30,000
milliseconds.

Again, all of the .Net services I wrote launch fine most of the time, but
on upon occasion they exhibit this error on the boot of the system. I
cannot detect anything different, as not all boots on the same system
exhibit the same error! And the services all seem to start manually once
the system is booted.

-ken

Add "Network Connections" to the dependencies.

Willy.
 
K

Ken Allen

Alan said:
Make sure that you have your service dependencies set correctly so that all
required services are running before yours...

-- Alan
But there are no dependencies that I am aware of, other than possibly
the Event Log.

-ken
 
K

Ken Allen

Alan said:
Sorry, I did not read your post carefully enough before my last post.

There are no services for .NET itself. Try adding RPC as a dependency for
your service. For this you enter RPCSS in your dependency list. If built
and (re)installed correctly, you should see "Remote Procedure Call (RPC)" in
the Dependencies tab for the service in the SCM.

-- Alan
I have added this to the code and we are testing now, but why would this
have an effect?

-ken
 
J

Jason

Having suffered through getting a C# service going, here's a trick that
I did that helped me track things down. In your OnStart function, put a
line at the beginning like "Thread.Sleep(20000)" and put a breakpoint at
the following line. Start your service, then quickly go back into
Visual Studio and hit Debug, Processes and attach to your service
(you'll probably need to check "Show system processes"). Once you do
that, your debugger is attached to the service and you can step through
once the Sleep is done and find out where things are going wrong.
 
K

Ken Allen

Interesting technique - but when my code fails, it does not seem to even
get that far, and when it succeeds, I have no need to debug it, and I
cannot predict when it will fail.

-ken
 
W

Willy Denoyette [MVP]

Ken Allen said:
Why, since the program does nothing with the network?

-ken

Not sure it doesn't, you use named pipes do you? Are your endpoints local
only? Note that if the Server service is running, all named pipes are
accessible remotely. If you only need local endpoints you should deny access
to NT_AUTHORITY\NETWORK or use local RPC.

Willy.
 
W

Willy Denoyette [MVP]

Ken Allen said:
I have added this to the code and we are testing now, but why would this
have an effect?

-ken

Because your named pipe endpoints are network endpoints that needs the RPC
service to run.
The server endpoints are registered with the RPC service.
The client of a named pipe establishes a connection with a pipe server by
calling the RPC service to resolve the server end-point.
Willy.
 
K

Ken Allen

OK, I have been over this so many times that I am getting dizzy. I have
modified the configuration to add dependencies, but this makes no
difference at all.

I am becoming convinced that the services that are failing (which are
all .Net code) are doing so before they even attempt to execute. The
only 'evidence' that I have of this is that when I added code to the
programs to create Event Log entries in the Main() routine, when the
service fails to start, not even these are present. This would indicate
to me that there is a problem with launching the executable to begin
with, even though the service is loading properly at installation time.

By the way, these services are being loaded as part of a larger
installation, which requires a reboot. The services are not started
immediately after installation, and the errors are first reported after
the first reboot after the installation. The number of reboots that are
performed before the services start properly vary, and there does not
seemm to be any pattern. Not all system exhibit this problem, as some
systems reboot and the services start fine the first time.

I am running out of things to try here. Anyone have any further suggestions?

-ken
 
W

Willy Denoyette [MVP]

Ken Allen said:
OK, I have been over this so many times that I am getting dizzy. I have
modified the configuration to add dependencies, but this makes no
difference at all.

I am becoming convinced that the services that are failing (which are all
.Net code) are doing so before they even attempt to execute. The only
'evidence' that I have of this is that when I added code to the programs
to create Event Log entries in the Main() routine, when the service fails
to start, not even these are present. This would indicate to me that there
is a problem with launching the executable to begin with, even though the
service is loading properly at installation time.

By the way, these services are being loaded as part of a larger
installation, which requires a reboot. The services are not started
immediately after installation, and the errors are first reported after
the first reboot after the installation. The number of reboots that are
performed before the services start properly vary, and there does not
seemm to be any pattern. Not all system exhibit this problem, as some
systems reboot and the services start fine the first time.

I am running out of things to try here. Anyone have any further
suggestions?

-ken

That would mean the process is not beeing created by the Service Control
Manager. The SCM creates a process using Win32 CreateProcessAsUser() using
the executable name and the user access token that represents the
useraccount as registered during install.
The token is obtained by the SCM by calling 'LogonUser' prior to the
'CreateProcessAsUser' call, using the credentials specified during service
install (if it's a user account).
When one of the calls to 'LogonUser' or 'CreateProcess' fails the SCM should
write an event to the System log, but it looks like you don't even see such
logs, right?
What happens when you start the service from the commandline using net start
<servicename>

Willy.
 
K

Ken Allen

Willy said:
That would mean the process is not beeing created by the Service Control
Manager. The SCM creates a process using Win32 CreateProcessAsUser() using
the executable name and the user access token that represents the
useraccount as registered during install.
The token is obtained by the SCM by calling 'LogonUser' prior to the
'CreateProcessAsUser' call, using the credentials specified during service
install (if it's a user account).
When one of the calls to 'LogonUser' or 'CreateProcess' fails the SCM should
write an event to the System log, but it looks like you don't even see such
logs, right?
What happens when you start the service from the commandline using net start
<servicename>

Willy.
If the services are set to manual or are installed and started after the
sytem is running, then all proceeds without error. This only occurs on a
boot, only on some systems, and does not always occur. Even on a system
where this does occur, it may cease to occur any any subsequent reboot!

-ken
 
T

ThiloL

I have the same problem. My service works on most machines, but on some
(slow) machines the service doesn't start on system startup.

Are there any news about this problem?

regards, thilo.
 
T

Thilo Langbein

I have the same problem with my service.

It seems that this problem occures only on a very slow machine (few RAM,
few CPU).

Are there any news concerning that problem?

Regards, Thilo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top