Called ServiceBase.Run(), but never receive OnStart()?

J

Jeff Dege

I have a service, written in C# and .NET V2.0.50727, that works fine on
some machines, but hangs on start on others.

In MyService.Main(), I read the registry for a few initialization
settings, most important of which for our purposes is the name of the
logfile.

I then create a new MyService object, and pass it to ServiceBase.Run().
(MyService is, of course, derived from ServiceBase).

I write a log message immediately before ServiceBase.Run(myServiceObject).

MyService.OnStart(args) writes a log message immediately on being called.

On the machines where this works, I see, in the logfile, the two messages
one immediately following the other.

On the machines where it does not, when I try to start the service, the
progress bar seems to stop, half-way, then after a long wait, I get a
failed to start dialog. In the Services Manager, the service displays a
"Starting" status, with the start, restart, and stop buttons greyed out.

If I try to remove the service, the Startup Type in the Services Manager
changes to "Disabled", but MyServicee stays in the list. Nothing short
of a reboot seems to work to get rid of it.

Meanwhile, the logfile shows nothing after ServiceBase.Run(). Either
MyService.OnStart() is not being called, or it's failing to write to the
logfile.

The event viewer shows only an uninformative message: "The MyService
service terminated unexpectedly".

I'm catching and logging all exceptions - none seem to be thrown.

I have no idea what is going on, and have no idea at all as how to figure
out what the problem is.

Since it shows up on one machine, but not on another, my guess would be
that it might be some sort of security or permissions issue. But guesses
aren't of much help. Is there anyway of tracking what is going on? SCM
is running into some sort of problem, where do I look to find out what it
is?



--
You have to sit there and accept one of the most galling things that a
bunch of dedicated individualists can ever realize -- that you were
trained to be individualists by the most relentless campaign of public
indoctrination in history, suckling your love of rebellion and
eccentricity from a society that -- evidently, at some level -- wants you
to be that way!
- David Brin
 
W

Willy Denoyette [MVP]

Jeff Dege said:
I have a service, written in C# and .NET V2.0.50727, that works fine on
some machines, but hangs on start on others.

In MyService.Main(), I read the registry for a few initialization
settings, most important of which for our purposes is the name of the
logfile.

I then create a new MyService object, and pass it to ServiceBase.Run().
(MyService is, of course, derived from ServiceBase).

I write a log message immediately before ServiceBase.Run(myServiceObject).

MyService.OnStart(args) writes a log message immediately on being called.

On the machines where this works, I see, in the logfile, the two messages
one immediately following the other.

On the machines where it does not, when I try to start the service, the
progress bar seems to stop, half-way, then after a long wait, I get a
failed to start dialog. In the Services Manager, the service displays a
"Starting" status, with the start, restart, and stop buttons greyed out.

If I try to remove the service, the Startup Type in the Services Manager
changes to "Disabled", but MyServicee stays in the list. Nothing short
of a reboot seems to work to get rid of it.

Meanwhile, the logfile shows nothing after ServiceBase.Run(). Either
MyService.OnStart() is not being called, or it's failing to write to the
logfile.

The event viewer shows only an uninformative message: "The MyService
service terminated unexpectedly".

I'm catching and logging all exceptions - none seem to be thrown.

I have no idea what is going on, and have no idea at all as how to figure
out what the problem is.

Since it shows up on one machine, but not on another, my guess would be
that it might be some sort of security or permissions issue. But guesses
aren't of much help. Is there anyway of tracking what is going on? SCM
is running into some sort of problem, where do I look to find out what it
is?



--
You have to sit there and accept one of the most galling things that a
bunch of dedicated individualists can ever realize -- that you were
trained to be individualists by the most relentless campaign of public
indoctrination in history, suckling your love of rebellion and
eccentricity from a society that -- evidently, at some level -- wants you
to be that way!
- David Brin



What exactly are you doing before calling ServiceBase.Run() and what are you
doing in your service class constructor?
Are you sure you call ServiceBase.Run(), before the 30 seconds time-out
period after the SCM started the service process?

Willy.
 
J

Jeff Dege

What exactly are you doing before calling ServiceBase.Run() and what are
you doing in your service class constructor? Are you sure you call
ServiceBase.Run(), before the 30 seconds time-out period after the SCM
started the service process?

Before ServiceBase.Run()? Read the registry for some initialization
settings. Write a startup message to the log file. Write an about to
call ServiceBase.Run() message to the log file.

In the service class constructor? Almost nothing.

Am I sure I call ServiceBase.Run() before the 30 seconds time-out period?

Yes. My log entries only show timestamps to the nearest second, but the
startup messages and the about to call ServiceBase.Run() log messages
always happen in the same second.

--
Nearly every electrical engineer believes deep in his heart that he
is better at writing computer software than any computer programmer,
and can show as proof the fact that he has written a number of small
applications, each of which was done quickly, easily, and exactly met
his needs.
 
W

Willy Denoyette [MVP]

Jeff Dege said:
Before ServiceBase.Run()? Read the registry for some initialization
settings. Write a startup message to the log file. Write an about to
call ServiceBase.Run() message to the log file.

But you are reading from the registry before you are actually writing to the
log file, what's important is to know the time you entered Main (before you
read from the registry) and the time at which you are calling Run, the
difference between those may not exceed 30 seconds. I would suggest you to
write a message to the eventlog when entering Main and calling Run.


Willy.
 
J

Jeff Dege

But you are reading from the registry before you are actually writing to
the log file, what's important is to know the time you entered Main
(before you read from the registry) and the time at which you are
calling Run, the difference between those may not exceed 30 seconds. I
would suggest you to write a message to the eventlog when entering Main
and calling Run.

So the reading of the registry settings is consistently taking just
enough time that the log message prior to ServiceBase.Run() occcurs in
less than 30 seconds, but the immediately following log message in
MyService.OnStart() does not?

Seems unlikely. I'll check it, but I'd be very surprised if this were
the issue.
 
W

Willy Denoyette [MVP]

Jeff Dege said:
So the reading of the registry settings is consistently taking just
enough time that the log message prior to ServiceBase.Run() occcurs in
less than 30 seconds, but the immediately following log message in
MyService.OnStart() does not?

Seems unlikely. I'll check it, but I'd be very surprised if this were
the issue.



But you don't log a message prior to the read from the registry, so you have
no idea when this happens. Anyway, fact is that your service does not start
and this fact is logged in the eventlog. Now there are two possible causes
for this; 1) the service control manager (SCM) did not receive a
registration request within 30 seconds after starting the service process,
or 2) the service did not signal it's state within 30 seconds after
receiving a start request from the SCM.


SCM --> start service process
service process -- 1 --> SCM
SCM -- 2 --> Service

If one of both 1 or 2 result in a time out, the service will be flagged as
"Failed to start" and a message will be logged in the eventlog.
So, you need to know when the service *process* was started, that is, when
Main was entered and compare this to the time when Run was called, the time
difference must be less than 30 seconds.

Willy.
 
J

Jeff Dege

But you don't log a message prior to the read from the registry, so you
have no idea when this happens. Anyway, fact is that your service does
not start and this fact is logged in the eventlog. Now there are two
possible causes for this; 1) the service control manager (SCM) did not
receive a registration request within 30 seconds after starting the
service process, or 2) the service did not signal it's state within 30
seconds after receiving a start request from the SCM.


SCM --> start service process
service process -- 1 --> SCM
SCM -- 2 --> Service

If one of both 1 or 2 result in a time out, the service will be flagged
as "Failed to start" and a message will be logged in the eventlog. So,
you need to know when the service *process* was started, that is, when
Main was entered and compare this to the time when Run was called, the
time difference must be less than 30 seconds.

Well, I finally got back to working on this problem.

Now I am logging prior to my reading from the registry, and as I had
suspected, there is no delay.

I log on startup, on parsing the command-line arguments, on accessing the
ini file, on opening the log file, etc. All of this, plus the messaging
in the log file that I had mentioned before, up to the call to
ServiceBase.Run(), occurs in the same second.

But MyService.OnStart() is never called. Or rather, on some machines it
is called immediately - in the same second as all the preceding startup
code, on others it is never called at all.


--
A human being should be able to change a diaper, plan an invasion,
butcher a hog, conn a ship, design a building, write a sonnet, balance
accounts, build a wall, set a bone, comfort the dying, take orders, give
orders, cooperate, act alone, solve equations, analyze a new problem,
pitch manure, program a computer, cook a tasty meal, fight efficiently,
die gallantly. Specialization is for insects.
- R. A. Heinlein
 

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