我在我的项目中遇到了同样的问题,在我的 Windows 服务项目中,我有以下app.config
部分:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="SDK" />
</assemblyBinding>
</runtime>
如果我将服务作为控制台执行,一切都很好,但是当我尝试安装它时 installutil 失败并且我得到了同样的异常,所以我的解决方案是通过命令行自行安装服务:像这样:
cmd: hotspotcenter -i <service_name="service name">
// service_name i made it optional
安装程序类助手:
internal static class BasicServiceInstaller
{
public static void Install(string serviceName)
{
CreateInstaller(serviceName).Install(new Hashtable());
}
public static void Uninstall(string serviceName)
{
CreateInstaller(serviceName).Uninstall(null);
}
private static Installer CreateInstaller(string serviceName)
{
var installer = new TransactedInstaller();
installer.Installers.Add(new ServiceInstaller
{
ServiceName = serviceName,
DisplayName = serviceName,
StartType = ServiceStartMode.Manual
});
installer.Installers.Add(new ServiceProcessInstaller
{
Account = ServiceAccount.LocalSystem
});
var installContext = new InstallContext(
serviceName + ".install.log", null);
installContext.Parameters["assemblypath"] =
Assembly.GetEntryAssembly().Location;
installer.Context = installContext;
return installer;
}
}
在服务项目的主条目中:
if (Environment.UserInteractive)
{
bool install = false;
bool uninstall = false;
string serviceName = "YourDefaultServiceName";
var p = new OptionSet()
.Add<bool>("i|install", "Install Windows Service", i => install = i)
.Add<bool>("i|install=", "Install Windows Service", i => install = i)
.Add<bool>("u|uninstall", "Uninstall Window Service", u => uninstall = u)
.Add<string>("sn|service_name=", "Service Name", n => serviceName = n);
p.Parse(args);
if (install)
{
BasicServiceInstaller.Install(serviceName);
return;
}
else if (uninstall)
{
BasicServiceInstaller.Uninstall(serviceName);
return;
}
// if no install or uninstall commands so start the service as a console.
var host = new YourService();
host.Start(args);
Console.ReadKey();
}
else
{
ServiceBase.Run(new HotspotCenterService());
}