Troubleshooting a NullReference exception in a service


I have a windows service that runs various system monitoring operations. However, when running SNMP related checks, I always get a NullReference exception. The code runs fine when run through the user interface (under my username and password), but always errors running as the service. I've tried running the service as different user accounts (including mine), with no luck. I've tried replacing the SNMP monitoring code with calling the PowerShell cmdlet get-snmp (from the /n NetCmdlets), but that yields the same error.

The application I'm working with is PolyMon.

Any ideas?


I have tackled these kind of issues before, if you haven't already found the answer, I suggest the following:

  • Enable tracing/logging in all third party apps and libraries you are using such that the errors are logged to files instead of stdout or stderr. Often times, you will find a clue from these.
  • Your Windows Service may be relying on some Windows networking set up to be in place before startup. This, can be due to environment (PATH, as others have suggested) or due to 'dependencies' on other services.


By : Jay

Some ways to debug:

  • Is there any additional information in the Windows events log?
  • I believe you should be able to listen to some kind of global-exception event like Application_Exception in windows services. I can't remember the exact name but you can atelast dump stack trace from there.
  • You should be able to start debugging the project in service mode.

Some code snippets/stack trace/information will definitely help.

By : chakrit

A couple of things we've seen - more about differences between interactive vs services, but might help...

One thing we've seen that does not seem relevant is the difference with what is on the user vs system path.

Another thing we've seen relates to temporary files - the service we had was creating lots in the windows\temp directory - we tracked this down when it had created something like 65000 of these files and thus hit the limit of what a directory can hold...

Regards, Chris

This video can help you solving your question :)
By: admin