Windows Service Starts and Immediately Stops

P

pbd22

Hi.

I know my windows service works when i run it in debug mode on my dev
machine.
It also works in release mode on my dev machine. But, when I move the
service to
a production server, it exits immediately with a start/stop/nothing to
do error.

What could be wrong?

My OnStart looks like this:

protected override void OnStart(string[] args)
{
InitIndexer();
}

and InitIndexer() is a lengthly bit of code making up the meat of the
service.

any ideas?

thanks.
 
D

Dave

Hi.

I know my windows service works when i run it in debug mode on my dev
machine.
It also works in release mode on my dev machine. But, when I move the
service to
a production server, it exits immediately with a start/stop/nothing to
do error.

What could be wrong?

My OnStart looks like this:

 protected override void OnStart(string[] args)
        {
            InitIndexer();
        }

and InitIndexer() is a lengthly bit of code making up the meat of the
service.

any ideas?

thanks.

services can be a bit of a pain to debug as you can't just attach a
debugger. as you'll want event logging before you release your app
anyway, mind as well just put it in now, and use it for debugging
also. you can then trace out exactly what you want in your code and
see where things go ary.

your log method is simple, something like:

private void LogEvent(string LogMessage, EventLogEntryType
LogEntryType)
{
if (!EventLog.SourceExists(_eventSource))
{
EventLog.CreateEventSource(_eventSource, _logAppName);
}

EventLog MyLog = new EventLog();
MyLog.Source = _eventSource;
MyLog.WriteEntry(LogMessage, LogEntryType);
}

with caller

LogEvent(string.Format("Error in Service:OnStart() {1}", ex.Message),
EventLogEntryType.Error);

dave
 
P

pbd22

Thanks Dave,

So, the code should look like this? :

protected override void OnStart(string[] args)
{
try
{
InitIndexer();
}
catch(Exception ex)
{
LogEvent(string.Format("Error in
VBIndexerService:OnStart() {1}", ex.Message),
EventLogEntryType.Error);
}
}
 
P

pbd22

OK.

Went to the event logs and see all sorts of COM object access denied
errors.
Seems the DLLs I registered in the DEV environment are not firing when
I
move everything to the production server (my guess).

Example:

Event Type: Error
Event Source: My Source
Event Category: None
Event ID: 0
Date: 1/7/2008
Time: 1:29:54 PM
User: N/A
Computer: My Computer
Description:
WindowsXYZServer.GetSpace() Access is denied. (Exception from HRESULT:
0x80070005 (E_ACCESSDENIED))

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.

Question:

If I register a DLL as a certain path in my DEV system, say:

C:/x/y/z/library.DLL

and then I move the windows service to:

C:/a/b/c/

and put the DLLs in

C:/a/b/c/library.DLL

does that fudge up everything?

Must the path of the DLL files in the development
system match the path of the DLL files on the live server?

Thanks.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The OnStart is suppose to be short, the solution to your problem execute
your method in another thread:

protected override void OnStart(string[] args)
{
new Thread( new ThreadStart( InitIndexer)).Start();
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

See my other post for a solution, Your problem is taht the window's service
control manager expect that the service startup be complete in a short
amount of time. Otherwise it stop it. You have to use a thread.
 
W

Willy Denoyette [MVP]

pbd22 said:
OK.

Went to the event logs and see all sorts of COM object access denied
errors.
Seems the DLLs I registered in the DEV environment are not firing when
I
move everything to the production server (my guess).

Example:

Event Type: Error
Event Source: My Source
Event Category: None
Event ID: 0
Date: 1/7/2008
Time: 1:29:54 PM
User: N/A
Computer: My Computer
Description:
WindowsXYZServer.GetSpace() Access is denied. (Exception from HRESULT:
0x80070005 (E_ACCESSDENIED))

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.

Question:

If I register a DLL as a certain path in my DEV system, say:

C:/x/y/z/library.DLL

and then I move the windows service to:

C:/a/b/c/

and put the DLLs in

C:/a/b/c/library.DLL

does that fudge up everything?

Must the path of the DLL files in the development
system match the path of the DLL files on the live server?

Thanks.


We can't help you if you don't tell us what the code is doing, only posting
a snippet that shows nothing isn't of any help.
From the above it looks like (suppose the eventlog entries have something to
do with the service) you are calling a COM server, for which the caller has
no access permissions. So, you'll have to adjust the Access Permissions for
the COM server using DCOMCNFG.

Willy.
 
P

pbd22

We can't help you if you don't tell us what the code is doing, only posting
a snippet that shows nothing isn't of any help.
From the above it looks like (suppose the eventlog entries have something to
do with the service) you are calling a COM server, for which the caller has
no access permissions. So, you'll have to adjust the Access Permissions for
the COM server using DCOMCNFG.

Willy.

Thanks Willy,

The code is writing to a number of text files.
The writing happens by calling a COM object which is a DLL reference
in the project.
Access to the COM objects is what is throwing the errors. I don't have
a COM server,
but COM DLLs in the same folder as the windows service.

How Do I prevent this error for COMs called locally?

Please let me know if you need other information otherwise, thanks for
your help.
 
P

pbd22

Ignacio -

Thanks.

I tried your code and the start/stop error disappeared
but the service is still not working. I notice that when
I go to task manager i don't see myservice.exe in the
processes list.

I think this is due to access to my COM objects. How
is this resolved?

Thanks.
 
W

Willy Denoyette [MVP]

pbd22 said:
Thanks Willy,

The code is writing to a number of text files.
The writing happens by calling a COM object which is a DLL reference
in the project.
Access to the COM objects is what is throwing the errors. I don't have
a COM server,
but COM DLLs in the same folder as the windows service.

How Do I prevent this error for COMs called locally?

Please let me know if you need other information otherwise, thanks for
your help.


Ok forget about DCOMCNFG as it's an in-proc server, the COM server however,
is calling into Win32 to access the folder using the credentials of the
service account, this account in does not have permissions to write to the
destination folder. That means you'll need to run your service in an account
with sufficient permissions to access the local folder/files.

Willy.
 
W

Willy Denoyette [MVP]

Peter Bromberg said:
Your "COM dll's" are what Willy is referring to when he uses the
interchangeable term "COM server". Dcomcnfig can be used to adjust the
Launch
permissions and remove the errors.
-- Peter

Peter,

Not exactly, I was assuming an out-proc server, but it looks like it's an
in-proc, so dcomcnfg is not applicable here.

Willy.
 
P

pbd22

Peter -

"run your service in an account"

How do I find this out?
How do I make sure the account has read/write permissions.
And, more importantly, able to read the amount of space on the disk
(per the original error)?

I see the following in my service code:

this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;

My service has LocalSystem access which seems to be sufficient
according to some
threads I have read.

Is this what you mean? If not, could you be specific about where I
need to make these changes?

THanks.
 
W

Willy Denoyette [MVP]

pbd22 said:
Peter -

"run your service in an account"

How do I find this out?
How do I make sure the account has read/write permissions.
And, more importantly, able to read the amount of space on the disk
(per the original error)?

I see the following in my service code:

this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;

My service has LocalSystem access which seems to be sufficient
according to some
threads I have read.

Is this what you mean? If not, could you be specific about where I
need to make these changes?

THanks.


This message:
is written to the eventlog by your service (Source: My Source), right?. This
means that you are dealing with a security issue, the caller has no access
to a resource, which one is up to you to tell us.

WindowsXYZServer.GetSpace() is probably the method you are calling,
questions are:
- who exactly writes the error to the eventlog?
- what is "WindowsXYZServer" referring to, a managed class method or a COM
method?
- what is GetSpace doing, what resource is it accessing and how?
- and ... are you sure you don't call into a DCOM server somehow?

Willy.
 
P

pbd22

is written to the eventlog by your service (Source: My Source), right?

no. The source as described in the "Services" window is not my
service.
It is "MyCompany OurProduct". The only EventLog info coming back from
my service is that it started successfully.
WindowsXYZServer.GetSpace() is probably the method you are calling,
questions are:
- who exactly writes the error to the eventlog?
- what is "WindowsXYZServer" referring to, a managed class method or a COM
method?

A COM Method.
- what is GetSpace doing, what resource is it accessing and how?

GetSpace is finding out if the system has enough disk space to write a
large file
to a local directory. I think it uses kernel32.
- and ... are you sure you don't call into a DCOM server somehow?

if by "server" you mean a computer that isn't the localhost, then I
think the answer is no. But, if that is not what you mean, then I am
not sure and could use some advice as to how to better answer you.
 
W

Willy Denoyette [MVP]

pbd22 said:
no. The source as described in the "Services" window is not my
service.


Question: Who's writing this message to the eventlog??

It is "MyCompany OurProduct". The only EventLog info coming back from
my service is that it started successfully.



A COM Method.
Right.

GetSpace is finding out if the system has enough disk space to write a
large file
to a local directory. I think it uses kernel32.


if by "server" you mean a computer that isn't the localhost, then I
think the answer is no. But, if that is not what you mean, then I am
not sure and could use some advice as to how to better answer you.

I'm talking about software components, not a computer, when talking about
COM (or any other distributed system) you have clents and servers, a clients
is the component that creates instances of a class and calls methods of that
class, the server is that part that implements the class. COM knows two kind
of servers, in-proc (DLL's) and out-proc servers. in-proc servers (DLL's)
are always loaded in the clients process, out-proc servers always run in a
process separate from the client (on the same or on another machine).
Question: - what kind of server is it (in or out-proc)?
- what's the language it is written in?
- did you register the server on the target system?
- who's the owner of the code of this COM server, he should be able to give
an answer to all these questions.

Willy.
 
P

pbd22

Question: Who's writing this message to the eventlog??





I'm talking about software components, not a computer, when talking about
COM (or any other distributed system) you have clents and servers, a clients
is the component that creates instances of a class and calls methods of that
class, the server is that part that implements the class. COM knows two kind
of servers, in-proc (DLL's) and out-proc servers. in-proc servers (DLL's)
are always loaded in the clients process, out-proc servers always run in a
process separate from the client (on the same or on another machine).
Question: - what kind of server is it (in or out-proc)?

You may not like this answer but it is both. I have numerous DLLs in
this
project that call COM objects. I also have an EXE called from inside
the same project
that runs an external process that, itself, calls COM objects. But,
this error relates
to in-proc I think.
- what's the language it is written in?
C#

- did you register the server on the target system?

By "register the server" do you mean using gacutil for the DLLs?
If that is what you mean, I think I have but I will check again.
- who's the owner of the code of this COM server, he should be able to give
an answer to all these questions.

Well, I am the owner of the C# code. But the COM objects were written
by
a programmer before me - in VB6. She is gone and I am left to call
these classes
from within my own code with out too much prior knowledge of the inner
workings of
the DLLs.

Peter
 
P

pbd22

OK,

First, I think it is worth mentioning that the program in
question is a windows service.

If you mean "register the DLLs" in your previous query,
I feel silly, but I don't think I actually registered the DLLs
on the target server. I registered the DLLs inside my
IDE and everything worked just fine so, I guess I didn't
think to do anything different on the target machine. My Bad.

So, I am guessing I have my three Interop DLLs to register
on the target machine. But, when I try the below command:

RegAsm.exe "C:\PATH\Interop.MYDLL.dll" /tlb:Interop.MYDLL.tlb

I get the following error:

Types registered successfully
RegAsm: error RA0000 : CLR assembly "C:\PATH\Interop.MYDLL.dll" was
imported from a type library and cannot be re-exported to a type
library. Made sure the type library from which the assembly was
imported is registered.

Do you think this is the solution to my permissions error and, if you
do, or, if
you think I should be doing this, then what am I doing wrong???

Thanks again.
 
W

Willy Denoyette [MVP]

pbd22 said:
OK,

First, I think it is worth mentioning that the program in
question is a windows service.

If you mean "register the DLLs" in your previous query,
I feel silly, but I don't think I actually registered the DLLs
on the target server. I registered the DLLs inside my
IDE and everything worked just fine so, I guess I didn't
think to do anything different on the target machine. My Bad.

So, I am guessing I have my three Interop DLLs to register
on the target machine. But, when I try the below command:

RegAsm.exe "C:\PATH\Interop.MYDLL.dll" /tlb:Interop.MYDLL.tlb

I get the following error:

Types registered successfully
RegAsm: error RA0000 : CLR assembly "C:\PATH\Interop.MYDLL.dll" was
imported from a type library and cannot be re-exported to a type
library. Made sure the type library from which the assembly was
imported is registered.
No, no, In a previous reply you said that the dll's were VB6 (native) COM
dll's, you don't (can't) have to registered these with regasm!!!!
Do you think this is the solution to my permissions error and, if you
do, or, if
you think I should be doing this, then what am I doing wrong???

Thanks again.



Sorry, but if you don't or can't answer the question "who's writing the
error message to the eventlog", it will be very hard to help you out with
this. So, you need to find out which components writes to the eventlog.
Also you keep "guessing" about what other components you are dealing with,
what started as a "Service stops immediately", has become; a service that
calls into COM that call into exe that calls back into COM.
You need to have a clear overview about the different components in your
solution, you need to know exactly which are managed and which are
unmanaged, which are COM and which are not.
You need to know exactly which one calls the other and what resources each
of them are accessing, one (or more) of them (the
WindowsXYZServer.GetSpace() method/function) does *not* have the appropriate
rights to access a resource, because it runs in a security context (an
account) that has no privileges to do what is supposed to be done in that
method.
With "register DLL's" in terms of native COM dll's or exe's, I mean that
COM servers (DLL's and EXE's) need to be registered on the target system, by
running regsvr32 <mynativecom.dll> in case of a DLL, or running the server
exe with the option register (somecomserver.exe /register) in case of an exe
COM server. You can't call into the COM objects without registering.
You also have an *exe* in the solution that gets *called* by ?? and calls
into COM. What kind of exe is this? how is it "called"? Is it getting called
in terms of method calls, or do you mean that it's getting started from
within another component?

Willy.
 

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