error while installing window service System.Security.SecurityException - c#

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

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".

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.

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)
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."
:)

Related

Username/Pass request during WinService installation

I have done a test Win service to make sure I have the Installer project (which is the part of that project and is set to InstallShield Limited by default) works fine.
I've searched for the correct manual and did the same as it was suggested:
Pls, see the WinService Installation class definition:
Then I tried both ways
1)
2)
Then I successfully build the Installation project and get the setup package..
As you can see everything is set to be installed under the Local user..
But each time I run the installation pask it asks for credentials..
My question is: how to avoid that dialog during installation?
Actually, I found the solution by myself.
The start point should be: To make yourself sure that the service is installable manually using InstallUtil.
In my case I mistakenly named ServiceName in AfterInstall event, when engaged Service Controller.. missed letter "1"
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
sc.Start();
}
}
After that I changed the settings in InstallShield Project..
1) I dropped the added service at screen Way#1
2) Add InstallClass as it shown on Way#2
The request of the credential was because inside the service itself, I missed setting LocalSystem for Property Account of Service Process Installer in the Design Mode.
So, now it works..

Installing MSI through windows service

I have a strange problem,
I have a WPF application. I have created a windows installer for this and installation will create application shortcut in user's start menu. I'm installing this MSI through windows service. Installation works fine through windows service but it doesnt create shortcut in start menu also i dont see this application under Programs and Features. But if i manually install this everything works fine. Any clue why this is happening ?
Code to execute MSI
Process installProcess = new Process();
//Assign required properties
installProcess.StartInfo.FileName = MSIEXEC;
installProcess.StartInfo.RedirectStandardError = true;
installProcess.StartInfo.UseShellExecute = false;
LogManager.Write("Process object is created");
//Create new StringBuilder instance
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(SYMBOL_SLASH);
stringBuilder.Append(SYMBOL_INSTALL);
stringBuilder.Append(installerPath);
stringBuilder.Append(SYMBOL_QN);
LogManager.Write("StringBuilder is created: " + stringBuilder.ToString());
installProcess.StartInfo.Arguments = stringBuilder.ToString();
installProcess.Start();
InstallAllUsers property in for MSI was set to false. My windows service was running under Local System account and my machine was logged in with windows authentication under administrator account. So when the installation happened it was assumed that MSI is installed by the user other than who has logged into the machine with windows authentication so it didnt not show up in the start menu and under Programs and Features.
It was not necessary for me to keep the InstallAllUsers to false so simply i made it true which solved my problem.

Application process after msi installer finished is started as SYSTEM user name which sometimes can not create com object

I have a visual studio installer project which installs a C# app, I have a custom action and code to run the process after install is complete.
Let's say the logged in user on windows machine is "john". Now when john runs the msi installer, I check the process in the taskmanager and it shows that msiexec.exe is the process name for installer and it is running as user "john"
The installer completes and runs the install app's process myapp.exe now when I check this process in taskmanager of windows it shows that myapp.exe is running as SYSTEM (which I do know what account is that and why its not running as john)
Problem
When myapp.exe runs as SYSTEM user it can not create com component instance of a component (iTunes in my case) which was already running as user john. If the component was not running then creating isntance of the iTunes is sucessful, otherwise it fails.
Question
So is it possible to make sure when installer runs as john, when it finishes it starts process myapp.exe as john and not as SYSTEM user ? Note that I do not asks user for password during installer.
Code that I run when installer completes
// Event handler for 'Committed' event.
private void MyInstaller_Committed(object sender, InstallEventArgs e)
{
try
{
Directory.SetCurrentDirectory(Path.GetDirectoryName
(Assembly.GetExecutingAssembly().Location));
Process.Start(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + "\\MyApp.exe");
}
catch
{
// Do nothing...
}
}
This most likely happens because you are running your custom action as deferred. i.e. it is running by default under the SYSTEM user account.
A solution is to make sure you launch it immediate, for example you can launch it using a published event on the "Finish" button from the last dialog of the installation. I don't know exactly how to add a published event in VS setup projects, or if it is possible, but you can easily add one in packages built with specialized setup authoring tools.
In the Form_Load event write like this:
string lUserName=Environment.GetEnvironmentVariable("USERNAME");
Then instead of getting system user account you will get windows login user name at the time of installing MSI.
Personally I did manage to resolve this by running explorer.exe parameterized with the path to my app's executable:
Process.Start(
new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = Context.Parameters["target"],
UseShellExecute = true
});

How can I register a winservice?

How can I register a windows service within c#.net?
Have a look at these
Creating a Basic Windows Service in
C#
Creating a Windows Service with C#
Creating a Windows Service in C#
To find out if the service is already installed, get the list of services that are installed, and see if yours is in it:
bool existsAlready = System.ServiceProcess.ServiceController.GetServices()
.Where(service => service.ServiceName == yourServiceName)
.Any();
To actually install it, you have to create an installer object and tell it your executable and service name. Something like:
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller
{
Account = ServiceAccount.LocalService
};
string executablePath = String.Format("/assemblypath={0}", "yourprogram.exe"));
InstallContext context = new InstallContext(null, new[] { executablePath });
var installer = new ServiceInstaller
{
Context = context,
DisplayName = yourServiceName,
Description = yourServiceName,
ServiceName = yourServiceName,
StartType = ServiceStartMode.Automatic,
Parent = serviceProcessInstaller,
};
installer.Install(new System.Collections.Specialized.ListDictionary());
This is more or less all that InstallUtil.exe would do with your classes if you did it the documented way.
To start or stop a service, use the ServiceController class.
Have a look at Writting Windows Service
Here (Easiest language to create a windows service) is a step-by-step set of instructions for creating a Windows service in C#. Steps 6-9 show how to prepare your service to be registered with the local machine. Step 9 discusses how to use InstallUtil.exe to install your service.
To take it a step further, you can refer to the step-by-step instructions here (How to make a .NET Windows Service start right after the installation?) in order to have your Windows service install itself from the command line, i.e., without having to use InstallUtil.exe. For example, to install the service, you'd use this
MyService.exe /install
To uninstall, you'd do this
MyService.exe /uninstall
How do you register a Windows Service during installation? question tells us about this great step-by-step tutorial:
Walkthrough: Creating a Windows Service Application in the Component Designer.
There are several ways. It depends on the context:
1) You can create a Windows Installer package to do this for production. I believe a Visual Studio setup project will do this.
2) For development, installutil.exe should do it, as Svetlozar Angelov already said.
3) If you really need to customise the installation somehow you can write your own .NET code to do it. Look at the System.ServiceProcess.ServiceInstaller class.

C# - windows service installer not registering service

I'm trying to use an installer for a Windows service, and would like to avoid using InstallUtil.exe. The installer appears to work correctly (the executable and dlls are in the correct directory), but the service doesn't appear under Computer Management.
Here's what I've done so far:
The service class name is the default - Service1.
In the Project installer, the ServiceName of the service installer matches the class name - Service1.
Under the Custom Actions, the primary output of the service was added to Install, Commit, Rollback, and Uninstall.
I'm using http://support.microsoft.com/kb/816169 as a reference.
Any ideas?
Does your service project have an Installer class? You should have one that looks something like this:
[RunInstaller(true)]
public partial class Service1Installer : Installer
{
public Service1Installer()
{
InitializeComponent();
ServiceProcessInstaller process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
ServiceInstaller serviceAdmin = new ServiceInstaller();
serviceAdmin.StartType = ServiceStartMode.Manual;
serviceAdmin.ServiceName = "Service1";
serviceAdmin.DisplayName = "Service1";
serviceAdmin.Description = "Service1";
Installers.Add(serviceAdmin);
}
}
Make sure you've created a ServiceInstaller and ServiceProcessInstaller class in your service project. (Check this link for more info).
Close computer management and the Services window, run your installer again, and reopen the Services window.
If that doesn't work, restart your computer. You might have some files locked.
It goes without saying that you probably need administrative privileges on the machine for this to work properly.
I think I've figured it out. It might be a bug with the Designer code, or perhaps I missed a step.
I think in the designer code, in the InitializeComponent() method, it's supposed to add:
this.Installers.AddRange(new System.Configuration.Install.Installer[] {this.serviceProcessInstaller1, this.serviceInstaller1});
It wasn't there, so I added this in the ProjectInstaller constructor:
Installers.Add(serviceInstaller1);
Installers.Add(serviceProcessInstaller1);
Now on installation, it's listed as a service in Computer Management.

Categories