86

I am trying to create a Windows Service, but when I try and install it, it rolls back giving me this error:

System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

I don't know what this means - my application has the bare minimum since I am just testing things out first.

My Installer Code:

namespace WindowsService1
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;
            processInstaller.Username = null;
            processInstaller.Password = null;

            serviceInstaller.DisplayName = "My Service";
            serviceInstaller.StartType = ServiceStartMode.Manual;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "My Service";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
        }

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
        }
    }
}

My Service Code:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        this.ServiceName = "My Service";
    }

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

    protected override void OnStop()
    {
        base.OnStop();
    }
}
5
  • 2
    "I set both Account and Password to null and Account to Local System." - How did you set the same property to be two different things...?
    – qJake
    Apr 28, 2011 at 20:14
  • ` processInstaller.Account = ServiceAccount.LocalSystem; processInstaller.Username = null; processInstaller.Password = null; `
    – michelle
    Apr 28, 2011 at 20:35
  • sry i didnt notice i said account twice :/
    – michelle
    Apr 28, 2011 at 20:36
  • Maybe if you posted some of your code we might be able to help you.
    – Mark Kram
    Apr 28, 2011 at 22:05
  • 12
    Try running the "installUtil" command with administrator rights. Feb 10, 2015 at 12:43

8 Answers 8

210

I got the same exception when trying to install a service from the command line when using installutil in Windows 7. The solution was to open the command line as Administrator and then run installutil.

Also you may find it easier to use a framework like TopShelf to host your services, because it manages all the setup configuration from the name and description of the service to how your recovery process will work. It also allows to easily start your service from inside the IDE when you are debugging it.

6
  • 6
    This was the fix for me. Cheers.
    – John
    Apr 12, 2013 at 19:51
  • 1
    Can't believe it happened to me. Again !
    – ilans
    Jul 7, 2014 at 12:57
  • 6
    This should ideally be set as answer for this! :)
    – Amogh Natu
    Jan 22, 2015 at 19:36
  • This was my issue. One service was fine but installing a second was failing. Dec 3, 2015 at 15:29
  • Thank you. This fix my problem
    – HKH
    Mar 14, 2021 at 23:57
59

Run your command prompt as administrator. It will solve your problem

1
  • 2
    This worked for me. Even though my account is in the Administrators group Nov 14, 2013 at 10:26
46

Run as Administrator

This is a very common issue that programmers are missing out on.

1
  • 2
    I wasted 2 hours. Thanks!
    – habib
    Aug 30, 2018 at 13:05
12

You are probably trying to install a service using

  1. A user account which does not have sufficient rights OR
  2. A user with Administrator privileges but did not run the Command Prompt in 'Administrator Mode'.

Specifically, the issue in this case is the creation of some EventLog registry keys during service install.

One way to fix this is to make sure that you are running the Command Prompt in Administrator mode. (Right-click > Run as Administrator)

I have also encountered some cases where this method still fails to solve the SecurityException problem due to some registry keys not having 'Full Control' permissions for Administrator accounts.

The following keys should have 'Full Control' set for Administrators in order for the service to be able to write to the EventLog:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application

This may be done by:

  1. Invoking the Windows Registry Editor
    • Run [Win + R]
    • Type 'regedit'
    • OK
  2. Navigate to a path listed above
  3. Right click on the desired path
  4. Make sure that both Read and Full Control permission checkboxes are ticked for Administrators
  5. Click Apply and OK
  6. Repeat the same process for the other path
10

I solve this same issue by opening the VS2013 Developer Console with administrative permissions.

5

If you are being prompted for a user name and password, then something, somewhere is set to Account = ServiceAccount.User - that's the only way that could (should) happen. Perhaps your code in the comment above is not being executed or it is being changed back by later executing code.

As far as your second paragraph, in general, I would think a service would be fine for this if you don't want it to be see on the console or run as a task. I am not sure if I understand the part about running it as ASP.NET and having it not allow you to see the database...

Finally, in your last paragraph, I can't speak to the NullExeception without knowing more about what is going on in your installer's code.

3
  • As for the second paragraph, I have a lot of code that is doing processing, as I mentioned, its extracting rss feeds, collecting the data, parsing it and storing it into a database. I have an ASP.NET web application because I am providing the user with an interface so that he/she can see recommendations based on the data in my database. If there is a better method that I can use a background process in ASP.NET, i would gladly choose that..is there a way?
    – michelle
    Apr 29, 2011 at 7:30
  • @michelle, Try removing the three lines of code that set the Account, Username, and Password. I am guessing that this is getting called after the installer looks at the properties and sees the default ServiceAccount.User, then asks for credentials, then you overwrite it with nulls. Try setting it on the property pages. Just a stab in the dark.
    – Jim
    Apr 29, 2011 at 19:28
  • 1
    @michelle, as far as the other part of your comment, you mentioned about running a "background process in ASP.NET". Sorry, this is getting beyond my expertise. IF what you are trying to do can be done, it will probably require AJAX. AJAX provides a much richer Web UI experinece (for example, Google Maps uses AJAX, probably as well as Google search as you type things in results just appear). You will probably have better luck starting a new question that is tagged specifically that that.
    – Jim
    Apr 29, 2011 at 19:33
3

I was getting this error(above in the OP) while trying to test for the existence of an EventLog

   if (!EventLog.SourceExists("applicatioName"))
         EventLog.CreateEventSource("applicatioName", "Application");

Running VisualStudio as Administrator resolved the issue.

0

In my case, I had a problem in the ProjectInstaller class, I tried to change a property of the service and this attribution generated an object reference error once the coding was wrong. Once I got the right code, the System.Security.SecurityException error was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.