我有以下 WiX 项目来安装我的服务:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="GUID" Name="SetupWinService" Language="1049"
Version="1.0.0.0" Manufacturer="SetupWinService"
UpgradeCode="GUID">
<Package InstallerVersion="200" Compressed="yes"
Languages="1049" SummaryCodepage="1251"
InstallPrivileges="elevated"/>
<Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="WinService" Name="My Windows Service">
</Directory>
</Directory>
</Directory>
<DirectoryRef Id="WinService">
<Component Id="WinServiceInstallation" Guid="GUID">
<File Id="ClientService.exe"
Name="ClientService.exe"
Source="...\ClientService.exe"
Vital="yes" KeyPath="yes" DiskId="1"/>
<File Id="App.config"
Name="App.config"
Source="...\App.config"
Vital="yes" KeyPath="no" DiskId="1"/>
<!--And some DLLs here-->
<ServiceInstall Id="ServiceInstaller"
Type="ownProcess"
Vital="yes"
Name="WcfServiceHost"
DisplayName="WcfServiceHost"
Description="Hosts Wcf Service"
Start="auto"
Account="LocalSystem"
ErrorControl="ignore"
Interactive="no">
</ServiceInstall>
<ServiceControl Id="StartService" Name="WcfServiceHost"
Start="install" Stop="uninstall" Remove="uninstall"
Wait="yes" />
</Component>
</DirectoryRef>
<Feature Id="Complete" Title="SetupWinService" Level="1">
<ComponentRef Id="WinServiceInstallation" />
<ComponentGroupRef Id="Product.Generated" />
</Feature>
</Product>
</Wix>
我可以安装我的服务,但安装后我无法启动它。它说:
服务启动失败。验证您是否有足够的权限来启动系统服务。
但是我以管理员身份运行我的安装程序(Windows 7 Professional)并且还禁用了UAC。此外,我可以通过命令提示符使用 instalutil.exe 安装和运行该服务(我的服务项目包括 Installer 类的实现,并且通常根据本文进行标记),并且在这种情况下可以正常使用该服务。
如果我将 ServiceControl 元素的 Wait="yes" 替换为“no”,则服务安装时不会出错,但不会启动。在这种情况下,我也无法手动启动服务,因为服务启动并立即停止,并显示消息“本地计算机上的服务已启动然后停止。某些服务如果无工作可自动停止”。
我在互联网上搜索了这个问题,但没有找到任何解决方案。
我如何解决它?
那是我的安装程序类的代码:
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller serviceProcessInstaller;
private ServiceInstaller serviceInstaller;
public ProjectInstaller()
{
this.serviceProcessInstaller = new ServiceProcessInstaller();
this.serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
this.serviceProcessInstaller.Username = null;
this.serviceProcessInstaller.Password = null;
this.serviceInstaller = new ServiceInstaller();
this.serviceInstaller.ServiceName = "ClientServicesHost";
this.serviceInstaller.StartType = ServiceStartMode.Automatic;
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
this.AfterInstall +=
new InstallEventHandler(ProjectInstaller_AfterInstall);
}
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("ClientServicesHost");
sc.Start();
}
}
还有我的 Windows 服务:
class WindowsClientService : ServiceBase
{
public ServiceHost serviceHost = null;
public WindowsClientService()
{
this.ServiceName = "WcfServiceHost";
}
public static void Main()
{
ServiceBase.Run(new WindowsClientService());
}
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
// Create a ServiceHost for WcfClientService type
// and provide the base address.
serviceHost = new ServiceHost(typeof(WcfClientService));
// Open the ServiceHost to create listeners
// and start listening for messages.
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
有人指出我的服务自动停止的原因 - 启动后它什么也不做。是真的吗?我的服务创建侦听器并开始侦听 - 那是“什么都不做”吗?