|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Catching exception errors in a C# Windows service?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. 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. > 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! 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. Looks like I answered my own question, placing the args routine in the
OnStart function instaed of Main fixed the problem. 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. > Sahil Malik [MVP] wrote:
> There is nothing special about a windows service, other than the fact While I agree with your statement about exception handling, I disagree > it is run by windows - not you (or the user). 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 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 I was referring to debugging it per this blog post of mine >> it is run by windows - not you (or the user). 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 > Sahil Malik [MVP] wrote:
> I was referring to debugging it per this blog post of mine Indeed, on my part too - I was trying to build on what you said. :-)> http://codebetter.com/blogs/sahil.malik/archive/2004/12/06/35295.aspx > .. i.e. Exception Handling only. > > Sorry for the confusion? :) Richard -- http://www.grimes.demon.co.uk/workshops/fusionWS.htm http://www.grimes.demon.co.uk/workshops/securityWS.htm |
|||||||||||||||||||||||