Home All Groups Group Topic Archive Search About

Catching exception errors in a C# Windows service?

Author
6 Oct 2005 12:32 AM
emdeusenet
Not sure if I have the correct group, but here goes.

I am trying to write a Windows service in c# and am having trouble
catching exceptions. I purposley tried to write to a bad file name
hoping that the exception would be caught and I could log to the event
log or external log file. For some reason when I use a try/catch block
nothing is "caught". I assume there is something else that is catching
exceptions and not passing to my catch block.

Any ideas?

Thanks.

Author
6 Oct 2005 3:56 AM
David Levine
That does not sound correct. It would be helpful to post a code sample that
demonstrates the problem.

<emdeuse***@yahoo.com> wrote in message
Show quote
news:1128558750.070703.135080@g44g2000cwa.googlegroups.com...
> Not sure if I have the correct group, but here goes.
>
> I am trying to write a Windows service in c# and am having trouble
> catching exceptions. I purposley tried to write to a bad file name
> hoping that the exception would be caught and I could log to the event
> log or external log file. For some reason when I use a try/catch block
> nothing is "caught". I assume there is something else that is catching
> exceptions and not passing to my catch block.
>
> Any ideas?
>
> Thanks.
>
Author
6 Oct 2005 4:07 PM
emdeusenet
I found out late last night that I was accidently stopping the service
in code just before the catch block, after I removed that everything
works fine :) Thanks for this link, I think it help me out a lot!
Author
6 Oct 2005 10:00 PM
emdeusenet
Sahil, now I have another question. If I wanted to add debug support by
referecing the "startup parameters" in the service control manager, for
some reason it does not work if I look for agrs in the Main procedure.
Anyone have ideas on how I could do this? My goal would be if a "debug"
parameter was specified in the control manager I would be able to log
certain events.

Here is what I have:

// in the class
public static bool debugMode = false;

....
static void Main(string[] args)
        {

            if ( args.Length > 0 )
            {
                if ( args[0].ToString() == "debug" )
                {
                    debugMode = true;
                }
            }

I don't think it is ever getting to getting to "debugMode = true;"

Thanks again.
Author
6 Oct 2005 10:30 PM
emdeusenet
Looks like I answered my own question, placing the args routine in the
OnStart function instaed of Main fixed the problem.
Author
6 Oct 2005 3:58 AM
Sahil Malik [MVP]
There is nothing special about a windows service, other than the fact it is
run by windows - not you (or the user). Exception handling shouldn't be any
different. Here is a trick to debug win-services, try and see if it helps.

http://codebetter.com/blogs/sahil.malik/archive/2004/12/06/35295.aspx

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----------------------------------------------------------------------------



<emdeuse***@yahoo.com> wrote in message
Show quote
news:1128558750.070703.135080@g44g2000cwa.googlegroups.com...
> Not sure if I have the correct group, but here goes.
>
> I am trying to write a Windows service in c# and am having trouble
> catching exceptions. I purposley tried to write to a bad file name
> hoping that the exception would be caught and I could log to the event
> log or external log file. For some reason when I use a try/catch block
> nothing is "caught". I assume there is something else that is catching
> exceptions and not passing to my catch block.
>
> Any ideas?
>
> Thanks.
>
Author
15 Oct 2005 4:58 PM
Richard Grimes [MVP]
Sahil Malik [MVP] wrote:
> There is nothing special about a windows service, other than the fact
> it is run by windows - not you (or the user).

While I agree with your statement about exception handling, I disagree
with this statement. For a start, the thread that runs the main service
function is not even the process's thread, it is created by the service
control manager (SCM) and is injected into the service.

Services are not started with CreateProcess, instead they are started by
the SCM. If the service does not complete it's initialization in a set
amount of time then the SCM will kill the service. There's also the
aspect of SCM control messages that are sent to the service, again,
totally different to how normal Win32 processes work.

Finally, services are designed to run under users other than the
interactive user (to be honest I cannot think of any reason why the
interactive user should run a service). This means that the service runs
in a Windows Station that is different to the one the interactive user
is using and so *everything* that has a GUI aspect will be invisible to
the interactive user, hence services should NEVER have a user interface.

So services are completely different to GUI or console processes.

Richard
Author
16 Oct 2005 3:25 AM
Sahil Malik [MVP]
Okay .. I can't disagree with what you say below, but when I said ..

>> There is nothing special about a windows service, other than the fact
>> it is run by windows - not you (or the user).

I was referring to debugging it per this blog post of mine
http://codebetter.com/blogs/sahil.malik/archive/2004/12/06/35295.aspx ..
i.e. Exception Handling only.

Sorry for the confusion? :)

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----------------------------------------------------------------------------



Show quote
"Richard Grimes [MVP]" <read my sig> wrote in message
news:OUSbmCb0FHA.2008@TK2MSFTNGP10.phx.gbl...
> Sahil Malik [MVP] wrote:
>> There is nothing special about a windows service, other than the fact
>> it is run by windows - not you (or the user).
>
> While I agree with your statement about exception handling, I disagree
> with this statement. For a start, the thread that runs the main service
> function is not even the process's thread, it is created by the service
> control manager (SCM) and is injected into the service.
>
> Services are not started with CreateProcess, instead they are started by
> the SCM. If the service does not complete it's initialization in a set
> amount of time then the SCM will kill the service. There's also the aspect
> of SCM control messages that are sent to the service, again, totally
> different to how normal Win32 processes work.
>
> Finally, services are designed to run under users other than the
> interactive user (to be honest I cannot think of any reason why the
> interactive user should run a service). This means that the service runs
> in a Windows Station that is different to the one the interactive user is
> using and so *everything* that has a GUI aspect will be invisible to the
> interactive user, hence services should NEVER have a user interface.
>
> So services are completely different to GUI or console processes.
>
> Richard
> --
> http://www.grimes.demon.co.uk/workshops/fusionWS.htm
> http://www.grimes.demon.co.uk/workshops/securityWS.htm
>
Author
16 Oct 2005 10:45 AM
Richard Grimes
Sahil Malik [MVP] wrote:
> I was referring to debugging it per this blog post of mine
> http://codebetter.com/blogs/sahil.malik/archive/2004/12/06/35295.aspx
> .. i.e. Exception Handling only.
>
> Sorry for the confusion? :)

Indeed, on my part too - I was trying to build on what you said. :-)

Richard

AddThis Social Bookmark Button