24

I created a window service and to install it I created its deployment project and installed that. After installing I stared it. It successfully started.

Next day I made some modification, and rebuild and reinstalled but now its not installing.

Then I thought its issue with installer, lets create a custom installer for service so that anytime I can update my code.

I created it like this incase if anyone need this for future.

public class MyInstaller : Installer
    {
        ServiceProcessInstaller spi;
        ServiceInstaller si;
        public MyInstaller()
        {            
            spi = new ServiceProcessInstaller();
            spi.Account = ServiceAccount.LocalSystem;

            si = new ServiceInstaller();
            si.StartType = ServiceStartMode.Manual;
            si.ServiceName = "MyService";
            si.DisplayName = "My Service";
            si.Description = "service installed from command line";

            this.Installers.Add(spi);
            this.Installers.Add(si);

        }
}

I called it from main method by check the parameter args.

                            case "-i":
                            case "-install":
                                ti = new TransactedInstaller();
                                mi = new MyInstaller();
                                ti.Installers.Add(mi);
                                string logPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\install.log";
                                ctx = new InstallContext(logPath, cmdline);
                                ti.Context = ctx; //.Context ( ctx );
                                ti.Install(new Hashtable());
                                break;

Now when I am trying to install. I recevied error System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

I google it, and come to know service will try to access application log while installing and write log there.

I am not writing any event log. I have my log4net for logging. But still its default behaviour.

How to overcome this issue now? Its not getting installed even I have all permissions.

Thanks

3 Answers 3

75

I have found that at times you may need to "Run as Administrator". If you are installing from a command prompt you may need to start that up with "Run as Administrator".

7
  • I did that using cntrl+shift+enter in run. Then again i manaully right clicked and run as admin and it worked man. thanks what a silly mistakes i am doing. but i would like to know how to improve start/stop performance of window service.
    – sunder
    May 3, 2012 at 6:37
  • @sunder: the start/stop performance is going to have a lot to do with what your service is doing. 'Start' should be OK since typically you woud spin up a new thread that does the work. To stop you would need some quick way to stop what the thread is up to. So there is no generic way to improve performance.
    – Eben Roux
    May 3, 2012 at 9:13
  • I created a batch file (.BAT) to install the service and I was running the batch as administrator but it was not working. Then, I run the command prompt as administrator and installed the service without the BAT file, just typing the name of the EXE file -install. I'm not sure why this happened but maybe it could help others. I think it could be related to the fact that running a BAT file as admin is not executing the code you write there as admin, just the BAT file. Sep 25, 2014 at 0:44
  • To get installutil.exe to work from regular Administrator: Command Prompt, do this from Developer Command prompt: 1) where installutil.exe 2) add path to System Properties, Advanced, Environment Variables... System variables, Path (in my case: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe) Jun 29, 2017 at 17:41
  • Cases like this why do you have to "run as admin" if you're logged in as part of the admin group? Confused.
    – chitgoks
    Sep 21, 2017 at 5:59
1

I might have had a separate root cause for the message, but I fixed it by changing my service to run as LocalSystem (which is what I want anyway) instead of LocalService.

0

I can confirm that under "windows 7 64 bit" AND "Windows 10" you must:

1) run Visual studio command prompt AS ADMINISTRATOR (right click.. Other.. tun as admin)

enter image description here

2) go to "obj" folder where You have the exe. (cd [all path to \obj] ) 3) launch installutil [myservice.exe]

if not run as "admin", it fails even on old win7. :(

note: MSDN does explain it:

(at: https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer)

"To install a Windows service, you must have administrative credentials on the computer on which you're installing it."

:)

Your Answer

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

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