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

8

89

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();
    }
}
Manado answered 28/4, 2011 at 17:1 Comment(5)
"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...?Unifilar
` processInstaller.Account = ServiceAccount.LocalSystem; processInstaller.Username = null; processInstaller.Password = null; `Manado
sry i didnt notice i said account twice :/Manado
Maybe if you posted some of your code we might be able to help you.Winn
Try running the "installUtil" command with administrator rights.Suzannesuzerain
S
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.

Sumter answered 28/4, 2011 at 22:27 Comment(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?Manado
@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.Sumter
@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.Sumter
T
214

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.

Toponym answered 9/3, 2012 at 14:19 Comment(1)
This was my issue. One service was fine but installing a second was failing.Laurettelauri
H
61

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

Hysterectomize answered 26/4, 2013 at 6:30 Comment(1)
This worked for me. Even though my account is in the Administrators groupStancil
G
49

Run as Administrator

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

Giffard answered 17/7, 2015 at 9:34 Comment(1)
I wasted 2 hours. Thanks!Carnivorous
T
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
Thrall answered 13/7, 2016 at 16:46 Comment(0)
F
10

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

Fateful answered 23/1, 2014 at 14:36 Comment(0)
S
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.

Sumter answered 28/4, 2011 at 22:27 Comment(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?Manado
@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.Sumter
@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.Sumter
T
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.

Tuning answered 24/9, 2019 at 18:45 Comment(0)
C
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.

Campanula answered 30/4, 2022 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.