.net 使用ServiceInstaller/ServiceProcessInstaller安装windows服务时处理依赖项
我正在尝试构建一个可自行安装的Windows服务。我有一个如下定义的安装程序类:.net 使用ServiceInstaller/ServiceProcessInstaller安装windows服务时处理依赖项,.net,deployment,windows-services,dependencies,installation,.net,Deployment,Windows Services,Dependencies,Installation,我正在尝试构建一个可自行安装的Windows服务。我有一个如下定义的安装程序类: [RunInstaller(true)] public class MyServiceInstaller : Installer { public MyServiceInstaller() { var serviceProcessInstaller = new ServiceProcessInstaller { Account
[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
public MyServiceInstaller()
{
var serviceProcessInstaller = new ServiceProcessInstaller
{
Account = ServiceAccount.LocalSystem
};
var serviceInstaller = new ServiceInstaller
{
DisplayName = "MyService",
StartType = ServiceStartMode.Automatic,
ServiceName = "MyService",
Description = "My Service Description"
};
Installers.Add(serviceProcessInstaller);
Installers.Add(serviceInstaller);
}
}
以下是我用于执行安装的代码:
public void Install()
{
using (var assemblyInstaller = new AssemblyInstaller(
typeof(MyService).Assembly,
null) {
UseNewContext = true
})
{
var state = new Hashtable();
try
{
assemblyInstaller.Install(state);
assemblyInstaller.Commit(state);
}
catch
{
try
{
assemblyInstaller.Rollback(state);
}
catch {}
throw;
}
}
}
从ASP.NET应用程序触发安装时,包含Windows服务的程序集将安装在以下位置:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\xxxxxxx\25c14c59\a945a1e6\assembly\dl3\abf52138\74461ad8_8d5bce01\MyService.EXE
问题是,此程序集是唯一安装的文件
问题:
通过根本不使用安装程序解决了这个问题。相反,我使用WMI(
Win32_服务
)注册我的服务,然后它只从原始位置运行。这里有一个相关的代码项目