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..
Related
To make my console application launch after install I have added an installer class to my project.
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(this.Context.Parameters["AssemblyPath"]) + #"\MyAppName.exe");
}
I have added custom actions to my SetupProject (shortcut to Primary output MyApp.exe under Install and Commit) to execute launch after install and it does launch the app.
However when the app launches after install, and Main() runs UserPrincipal.Current.EmailAddress; I get this error:
Unable to cast object of type
'System.DirectoryServices.AccountManagement.GroupPrincipal'
to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
(This is on my workplace network by the way).
Now if I go to the application folder and run the .exe.... it works.
So my question is: what is it about the "run after install" method that's making this happen and how can I fix it?
I've tried almost every corner of the internet but cant get my head around this and many other solutions haven't worked.
I've tried solutions from this SO post with no luck.
I've tried getting the email address from outlook instead but I get a different error (which doesnt happen during debug or from clicking the .exe).
My solution (which contains a dozen projects) works perfectly in Visual Studio 2013.
In Visual Studio 2017, I can open the solution and compile it.
But if I start the debug, I systematically get this error message:
The security debugging option is set but it requires the Visual Studio
hosting process which is unavailable in this debugging configuration.The
security debugging option will be disabled. This option may be re-enabled in
the Security property page. The debugging session will continue without
security debugging
And then, nothing happens. Nothing starts.
For information, this is a solution with multiple startup projects (including a WPF project).
Edit :
By disabling the option "Enable ClickOnce security settings" under Project -> Properties -> Security tab, it works.
This solved my issue:
Most likely, you have accidentally gotten the bit flipped to debug
with ClickOnce security settings. Can you get the project properties
for your app, go to the "Security" tab, and make sure to uncheck
"Enable ClickOnce Security settings" or check the "This is a full
trust application" radio button.
In case it helps anyone else - I have the same scenario - a multiple startup solution that includes a client that will be deployed with ClickOnce. To eliminate the problem that the client doesn't start after getting the Security Settings dialog, I moved it higher in the list in the startup projects dialog. If the client project is above the server project in the list, no error, everything debugs. If the client project is below the server project, then I get the error and the client never opens. This doesn't exactly SOLVE the problem but is a perfectly adequate workaround for me.
EDIT: You might need to close and reopen your Visual Studio for this workaround to be effective.
I spent hours trying to figure out the issue, this resolved it.
Go to Projct > Properties... > Build
Uncheck the checkbox Prefer 32-bit
MS have removed the VS hosting process in VS2017 - see
https://vslive.com/Blogs/News-and-Tips/2017/02/Debugging-Visual-Studio-2017-aims-to-speed-up-your-least-favorite-job.aspx
Because of this changing the EnableSecurityDebugging setting in the project user file to True simply results in the Error dialog appearing again at run-time.Clicking on OK in the dialog changes the user file setting back to False.
AFAIK there is no workaround although MS seem to be posting very frequent VS updates (latest is 15.3) In the meantime ClickOnce apps. will be unable to use the security debugging option.
This could likely be a glitch in some configuration file. The "Enable ClickOnce security settings" was already unmarked in the project settings but still this dialogue appeared every time the application was started. I did the following to get rid of this dialogue:
Open the project->security setting page
Mark "Enable ClickOnce security settings"
Unmark "Enable ClickOnce security settings"
Save the properties and start the application again
Properties
Here's a workaround that enabled me to debug my ClickOnce app. in VS2017 without getting the error message "Unable to determine identity of caller" when accessing Isolated Storage. The workaround should also work in any situation that requires the ClickOnce security settings.
To recreate the settings that were previously generated when the Enable ClickOnce security settings on the Security tab of the project's properties was checked, do the following:
1.Uncheck Enable ClickOnce security settings on the Security tab of your project's properties
2.Add the following to your App.Config file if not already present
<runtime>
<NetFx40_LegacySecurityPolicyenabled="true"/>
</runtime>
3.Add a reference to Microsoft.Build.Tasks.v4.0 to your project
The code to recreate the ClickOnce settings can go anywhere, but the following sample Main method illustrates the general idea
using System;
using System.Reflection;
using System.Runtime.Hosting;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Windows.Forms;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
namespace SecurityDebuggingTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0 && args[0] == "startui")
{
Application.Run(new Form1());
}
else
{
PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
string AppName = Assembly.GetEntryAssembly().GetName().Name;
string AppExe = $"{AppName}.exe";
string DebugSecurityZoneURL = $"{AppExe}.manifest";
string AppManifestPath = $"{AppName}.application";
string appType = "win32";
AssemblyIdentity ca = AssemblyIdentity.FromManifest(AppManifestPath);
string appIdentitySubString = $"Version={ca.Version}, Culture={ca.Culture}, PublicKeyToken={ca.PublicKeyToken}, ProcessorArchitecture={ca.ProcessorArchitecture}";
string assemblyIdentity = $"http://tempuri.org/{AppManifestPath}#{AppManifestPath}, {appIdentitySubString}/{AppExe}, {appIdentitySubString},Type={appType}";
System.ApplicationIdentity applicationIdentity = new System.ApplicationIdentity(assemblyIdentity);
ApplicationTrust appTrust = new ApplicationTrust();
appTrust.DefaultGrantSet = new PolicyStatement(permissions, PolicyStatementAttribute.Nothing);
appTrust.IsApplicationTrustedToRun = true;
appTrust.ApplicationIdentity = applicationIdentity;
AppDomainSetup adSetup = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
ActivationArguments = new ActivationArguments(
ActivationContext.CreatePartialActivationContext(
applicationIdentity,
new string[] { AppManifestPath, DebugSecurityZoneURL })
),
ApplicationTrust = appTrust
};
Evidence e = new Evidence();
e.AddHostEvidence(appTrust);
AppDomain a = AppDomain.CreateDomain("Internet Security Zone AppDomain", e, adSetup, permissions);
a.ExecuteAssembly(AppExe, e, new string[] { "startui" });
}
}
}
}
You may see the warning message about the VS Hosting process being unavailable when you first run the above code but thereafter the EnableSecurityDebugging setting in your project's user file will have been set to False and the code should run as normal.
Thanks to Microsoft's ClickOnce team for their help on this workaround.
I have yet another cause for why this message may come up. In my case, while testing cloning my solution from Git, I noticed that Visual Studio decided to set the Active solution platform to "Any CPU", whereas my startup project is explicitly targetting "x86". This caused the startup project to not build when I ran the build solution command.
Checking the Build box in the Configuration Manager for that project got rid of the error message.
In case anyone asks, I don't remember exactly why that one project is explicitly targetting x86.
I just had the same issue. Prefer 32-Bit was disabled.
I looked in the Output Path and it was bin\Release.
I created a bin\debug path and set the Output Path to this.
Resolved.
For me the solution was to switch to "The application is available offline as well" in Publish tab of project properties
Before I had "The application is available online only"
My issue seemed to be associated with the folder that the solution was in. My DEV branch solution worked without issue but the CERT branch (different local folder) gave the Security Debugging message when the ClickOnce Security settings were checked.
My solution: launch VS2019 as Admin. Problem is gone when debugging. And now I can launch without Admin and debugging is still good.
A quick solution with no explanation on why: Running my application in "Debug" configuration stopped the error and allowed my application to run.
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."
:)
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.
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.