I have a created a windows service and i am installing it using installutil. In the project installer i am creating a custom event log. But when my service starts my all logs are going to the "Application" instead of my cuustom log. Below is the code which i have added to installer.
// Create Event Source and Event Log
EventLogInstaller logInstaller = new EventLogInstaller();
logInstaller.Source = "MyServices";
logInstaller.Log = "MyService Events";
Installers.Add(logInstaller);
Furthurmore the name of service is MyService.exe.
When I uninstall and re-install the service, i installation fail with the following install log;
Running a transacted installation.
Beginning the Install phase of the installation. See the contents of
the log file for the D:\MyService\MyService\bin\Release\MyService.exe
assembly's progress. The file is located at
D:\MyService\MyService\bin\Release\MyService.InstallLog.
An exception occurred during the Install phase.
System.ArgumentException: Source MyServices already exists on the
local computer.
The Rollback phase of the installation is beginning. See the contents
of the log file for the
D:\MyService\MyService\bin\Release\MyService.exe assembly's progress.
The file is located at
D:\MyService\MyService\bin\Release\MyService.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
And this is how i write log entry;
EventLog.WriteEntry("MyServices", logMessage, logType);
Can someone please help me what wrong i am doing.
When you add a new Log Source you have to restart the server for the log source to be found properly.
You also need your installer to be aware of if the log source is already there.
So add a quick check for the log source before you add it and restart the server after installing the first time to get the log source to work.
Related
I have developed a windows service in c#, The project works fine in DEBUG mode, also the project is built with out any errors. When I try to install the service using InstallUtil.exe, I get below error and the service is not installed. Could someone please help what is going wrong here.
Installation command
Open command prompt in admin mode
Navigate to C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
Run below command
installutil.exe C:\Users\Public\TestService\MyService\bin\Release\MyService.exe
Error
Creating EventLog source MyService in log Application...
An exception occurred during the Install phase.
System.Exception: Value cannot be null.
Parameter name: path1
Below is the complete installation log.
Microsoft (R) .NET Framework Installation utility Version 4.8.4161.0
Copyright (C) Microsoft Corporation. All rights reserved.
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the C:\Users\Public\TestService\MyService\bin\Release\MyService.exe assembly's progress.
The file is located at C:\Users\Public\TestService\MyService\bin\Release\MyService.InstallLog.
Installing assembly 'C:\Users\Public\TestService\MyService\bin\Release\MyService.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\Users\Public\TestService\MyService\bin\Release\MyService.exe
logfile = C:\Users\Public\TestService\MyService\bin\Release\MyService.InstallLog
Installing service MyService...
Service MyService has been successfully installed.
Creating EventLog source MyService in log Application...
An exception occurred during the Install phase.
System.Exception: Value cannot be null.
Parameter name: path1
The Rollback phase of the installation is beginning.
See the contents of the log file for the C:\Users\Public\TestService\MyService\bin\Release\MyService.exe assembly's progress.
The file is located at C:\Users\Public\TestService\MyService\bin\Release\MyService.InstallLog.
Rolling back assembly 'C:\Users\Public\TestService\MyService\bin\Release\MyService.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\Users\Public\TestService\MyService\bin\Release\MyService.exe
logfile = C:\Users\Public\TestService\MyService\bin\Release\MyService.InstallLog
Restoring event log to previous state for source MyService.
Service MyService is being removed from the system...
Service MyService was successfully removed from the system.
Attempt to stop service MyService.
The Rollback phase completed successfully.
The transacted install has completed.
The installation failed, and the rollback has been performed.
did you try to go into your path : C:\Users\Public\TestService\MyService\bin\Release (if you built in debug use the debug folder)
and here, use command : installutil MyService.exe
You said the projet work well in debug mode. can you install it in debug ?
So I am trying to create a service using C# in Visual Studio 2017 and I keep getting an error when I try to install it.
Right now, I don't even have anything in my service, I just want to be able to install the service. I created a new Project in Visual Studio 2017 using the Windows Service (.NET Framework) template under Visual C# > Windows Desktop. I then added the installer via Right Click > Add Installer. For serviceProcessInstaller1, I set the account to LocalSystem. And...that's it!
Now, I try to install it. I open up command prompt (run as administrator) and I do
InstallUtil ServiceTest.exe
Things start off smoothly, but then I get a message:
"An exception occured during the Install phase.
System.UnauthorizedAccessException: Attempted to perform an
unauthorized operation."
What could be generating this message?
I've googled and tried the following things:
Ensure I run command prompt as administrator.
Assign Full Control to
the project folder to my admin account.
Configure permissions to Full
Control for admin account in the security log of regedit.
Log into my
computer as an administrator (rather than regular user) and run it
that way.
Despite these things, I keep getting the same error. What else could I be doing wrong? Would really appreciate some advice!
Note: I am trying to install this service locally on my computer.
Edit: Per request, here are the logs. I named my project "FailedService", by the way, since I can't get it to work!
InstallUtil.InstallLog
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe assembly's progress.
The file is located at C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog.
An exception occurred during the Install phase.
System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.
The Rollback phase of the installation is beginning.
See the contents of the log file for the C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe assembly's progress.
The file is located at C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
FailedService.InstallLog
Installing assembly 'C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog
assemblypath = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe
Installing service Service1...
Creating EventLog source Service1 in log Application...
Rolling back assembly 'C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog
assemblypath = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe
Restoring event log to previous state for source Service1.
I was FINALLY able to resolve this issue. I was able to get to the bottom of it by using SysInternal's Process Monitor. I opened up the program and had it take logs for InstallUtil.exe. From there, I began to search for any logs that said "Access Denied". As I sorted through them, I noticed something interesting: it was trying to create a registry key, but it kept failing!
So I went into the registry and created a key named "ServiceTest". After that, I ran the InstallUtil command again, and it worked!
If anyone else is having trouble, the solution is to create a key named after your process name in the following location:
HKLM\System\CurrentControlSet\Services\EventLog\Application\
Enter the full path to installUtil & your service binary in an elevated prompt, that could do.
I am trying to start service using the below code. This works fine for 99% machines but i get this issue on user machines. Any help to be able to reproduce this error or why this issue happens.
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.Running ||
sc.Status == ServiceControllerStatus.StartPending)
{
sc.WaitForStatus(ServiceControllerStatus.Running);
Logger.Info("Service already running");
return true;
}
sc.Start();
Error I receive is
System.InvalidOperationException: Cannot start service on computer
'.'. ---> System.ComponentModel.Win32Exception: Access is denied
I am running with Administrator privileges
When creating the service I also run sc sdset command to make service start stop by non admin processes.
There is a whole thread about it in social msdn. The issue persisted for numerous users and it seems you do not have enough privileges to start the service, in which case you will have to change the service into Administrative account:
make sure that the service is set to Local Account by:
Rightclick on the property(in Services.msc panel).
select the Log on option
And then check again to see if it's working.
I am assuming you are trying to achieve a scenario similar to this, i.e., install a service and start it automatically.
Assuming you have made sure that you are really running as Administrator, i.e., the Admin command prompt or Run As Administrator.
Also, assuming you have restarted the machine to make sure that old version of your service is really removed, as you have tried multiple times to install / uninstall your service.
The error Access is denied essentially means that the user running it does not have access. Since you have said that you are running as Administrator, it is possibly that even Administrator does not have access to start the service. May be you are in a locked down environment (likely via Group Policy), where only the Domain Administrator is the "most powerful" !
The next step would be to investigate the permissions your service has. A useful tool for this is: SubInAcl
https://ss64.com/nt/subinacl.html
Display or modify Access Control Entries (ACEs) for file and folder Permissions, Ownership and Domain.
SubInAcl /service "your service name"
The above command is not easy to use ! You need to download it from the Microsoft web site.
Another useful tool is the SC command. This would normally be available by default.
Service Control - Create, Start, Stop, Query or Delete any Windows SERVICE.
SC sdshow "your service name"
would give details of the permission.
This would give you data which can help in further investigation.
You could also start / stop the service with this command. You can try with this to check whether you are getting the same exception when using this tool as well.
The following serverfault question gives some details about setting permission to a service
https://serverfault.com/questions/187302/how-do-i-grant-start-stop-restart-permissions-on-a-service-to-an-arbitrary-user
If you want to see the Stack trace from installutil you can use the /ShowcallStack option
https://learn.microsoft.com/en-us/dotnet/framework/tools/installutil-exe-installer-tool
/ShowCallStack
Outputs the call stack to the log file if an exception occurs at any point during installation.
This is not an answer, it is my further exploration of the question.
Here is the log
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the
D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.
An exception occurred during the Install phase.
System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of Flight.ServiceHost.Installation.
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at Flight.ServiceHost.Installation.Install(IDictionary stateSaver) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 36
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.AssemblyInstaller.Install(IDictionary savedState)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.TransactedInstaller.Install(IDictionary savedState)
The inner exception System.InvalidOperationException was thrown with the following error message: Cannot start service PreFlight on computer '.'..
at System.ServiceProcess.ServiceController.Start(String[] args)
at System.ServiceProcess.ServiceController.Start()
at Flight.ServiceHost.Installation.OnAfterInstall(IDictionary savedState) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 49
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: Access is denied.
The Rollback phase of the installation is beginning.
See the contents of the log file for the D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
[Update]
I should mention that I am running WIndows 10
I managed to create an installer using an installer project, which does work.
The issue by me was RunningAsLocalService in topshelf, hope this helps someone.
I am trying to Install a windows service I made with the following command :
C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug>InstallU
til.exe filechecker.exe
but I am getting the following message:
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.exe assembly's progress.
The file is located at C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.InstallLog.
An exception occurred during the Install phase.
System.ArgumentException: Must specify value for source.
The Rollback phase of the installation is beginning.
See the contents of the log file for the C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.exe assembly's progress.
The file is located at C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.exe assembly's progress.
The file is located at C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.InstallLog.
An exception occurred during the Install phase.
System.ArgumentException: Must specify value for source.
The Rollback phase of the installation is beginning.
See the contents of the log file for the C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.exe assembly's progress.
The file is located at C:\$(MyFiles)\Projects\Win Service\c#\filechecker\filechecker\bin\Debug\filechecker.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
How can I solve this issue?
A ServiceInstaller always1 creates an event log source, and sets the Source name to the ServiceName. The error suggests to me that you're not setting the ServiceName on your ServiceInstaller instance (although I'd have hoped for a better error).
1Unless you manually iterate its installers collection and remove it, after it's instantiated. You'd also have to make sure you turn off all of the automatic logging and/or manually add your own EventLogInstaller using the same source name - this can be useful if you wish all logging from the service (including automatic logging) to go to somewhere other than the Application event log.
I need to uninstall a Windows Service I have created, but I get this error using the "Uninstall or change program" program in windows:
Error. An exception occurred while
uninstalling. This exception will be
ignored and the uninstall will
continue. However, the application
might not be fully uninstalled after
the uninstall is complete. -> The
event log source '111 My Service'
cannot be deleted, because it's equal
to the log name.
Then I click "OK" and the program remains listed in the list of installed programs. What is worse, I cannot install a new version of it. The windows installer says that another version of this product is already installed and I should uninstall it first. How do I get rid of this program?
Update Here is what is happening when I run InstallUtil.exe /u command on it.
The uninstall is beginning. See the
contents of the log file for the
C:\MyService.MyService. assembly's progress. The file
is located at
C:\MyService.MyService.InstallLog.
Uninstalling assembly
'C:\MyService.MyService.exe'. Affected
parameters are: logtoconsole =
assemblypath = C:\MyService.exe
logfile =
C:\MyService.MyService.InstallLog
Removing EventLog source 111 My
Service. An exception occurred during
the uninstallation of the
System.Diagnostics.EventLogInstaller
installer.
System.InvalidOperationException: The
event log source '111 My Service'
cannot be deleted, because it's equal
to the log name. An exception occurred
while uninstalling. This exception
will be ignored and the uninstall will
continue. However, the application
might not be fully uninstalle l is
complete.
The uninstall has completed. An
exception occurred while uninstalling.
This exception will be ignored and the
uninstall will continue. However, the
application might not be fully
uninstalle l is complete.
Try to execute next command in cmd:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u "c:\myservice.exe"
The second option is:
sc delete <service name>
After running the line above you can try to remove the service branch as well in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services.
The accepted answer didn't work for me.
What worked is open regedit in administrator mode and find the service and delete it.
I had the exact same problem (and nothing in the selected answer worked).
Surprisingly, what fixed the problem was (using the standard "Uninstall or change program" or "Programs and Features" windows dialog) right-clicking on the program and choosing repair (this was apparently successful), and then uninstalling.
This just happened to me and the solution above didn't work where you run the InstallUtil.exe or delete the service. I had initially set up my service with the "LocalSystem" security context, and it was installed with that. I later changed the security context to "User" and recompiled the service executable and tried to uninstall the old service. The error messages were coming up that are described here, and once I went and changed the security context back to "LocalSystem" I could fully uninstall.
In other words, you might want to uninstall your old service with the executable it was created with rather than a newer one with possible modifications.