13

I am doing the walkthrough in the following link: http://msdn.microsoft.com/en-us/library/zt39148a%28VS.80%29.aspx

I have followed it exactly, line by line. I installed the service successfully, however, when I try to run it, the following error message appears:

"An unhandled exception ("System.Security.SecurityException') occurred in MyNewService.Exe [5292].

I have seen that for many people it works, but some people get this exception, though I could not find an answer. Does anyone have an idea? Thanks.

sbenderli
  • 3,654
  • 9
  • 35
  • 48
  • What ID are you using? Have you granted it log on as a service rights? – serialhobbyist Sep 03 '09 at 14:57
  • Do you have the line of code where the error occurs in the Error Message? That will give a hint as to where the exception is occurring and let us give you more specific answers. – David Sep 03 '09 at 15:07
  • THe exception is thrown at: System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog"); And the exception is: "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security." – sbenderli Sep 08 '09 at 12:48

4 Answers4

19

The EventLog.SourceExists method is what will be causing this exception. The most common reason being it tries to access ALL the event logs (including the Security log) which by default in Vista you will not have permissions for. Another reason can be if the source you are looking for is not found in the event log (which I find rather odd!).

A work-around:

bool sourceFound = false;
try
{
    sourceFound = EventLog.SourceExists("MySource");
}
catch (SecurityException)
{
    sourceFound = false;
}

Another option is to simply elevate your permissions, however, as you where following the tutorial step by step your service would be running under the LocalService account (which again won't have permissions for this particular method). Hence, you will find on the MSDN documentation the solution is to check whether the event source exists in the ServiceInstaller and if it doesn't, create the source in the installer.

James
  • 80,725
  • 18
  • 167
  • 237
  • No problem, I had the same issue so I did a little investigation and it seemed this was the cause. The reason it seems to work in the installer is you will have to elevate your permissions so you are going to have the correct priveledges at that point. – James Oct 14 '09 at 17:52
  • good example of how to code installer http://blog.kappasolutions.ca/blog/post/2010/07/04/How-To-Create-a-Custom-Event-Log-in-Windows-7.aspx – Sergio Jan 28 '11 at 13:37
1

Are you a local administrator on your machine? If so, put the following line of code at the top of the constructor of your windows service:

System.Diagnostics.Debugger.Break();

When the service starts to run, it'll hit this breakpoint, allowing you to jump into Visual Studio. You can then debug from there until you discover where the exception is occurring.

Matt Davis
  • 45,297
  • 16
  • 93
  • 124
0

Go get a copy of Process Monitor and watch what happens - you should get the issue showing up as a failure result and you can investigate from there...

edit: To clarify, its a free tool from microsoft (sysinternals) and it does what it says on the tin :)

chrisb
  • 2,200
  • 1
  • 20
  • 23
  • I did what you suggested, but when I try and start the service, nothing shows up in Process Monitor under the name MyNewService..would another service be calling it? – sbenderli Sep 03 '09 at 16:11
0

Just a guess: Is your .exe file lying in a network folder? Or is is marked as "downloaded from the Internet"? Because, in that cases, the .NET Framework will assign less permissions to it than when it was directly on a local drive and not marked as downloaded from the internet.

mihi
  • 6,507
  • 1
  • 38
  • 48
  • 1
    The exe is actually installed on the computer. I made the installer as described in the original Microsoft page. – sbenderli Sep 04 '09 at 13:21
  • as far as I know the installer does not prevent you from installing the service to a folder on a network share. Maybe that changed in 2008, I don't have 2008 prof available here, only 2008 express (cannot build setup projects) and 2005 prof. – mihi Sep 05 '09 at 00:36
  • I've double-checked and the installation is local and not in a network folder. – sbenderli Sep 08 '09 at 12:46
  • in that case sorry, I have no other idea :( – mihi Sep 08 '09 at 17:18