WCF宿主与服务托管
若要完成对服务宿主的安装,我们还需要创建它的安装程序。我们可以自定义一个安装类,使其继承自System.Configuration.Install.Installer类。更简单的办法则是通过Windows服务提供的设计时支持,直接创建安装类。方法是在Windows服务例如DocumentsExplorerWindowsService的设计器视图下,通过单击右键,在快捷菜单中选择“Add Installer”,如图二所示:

创建的安装程序ExplorerServiceInstaller如下所示:
namespace BruceZhang.WCF.DocumentsExplorer在ExplorerServiceInstaller类中,ServiceAccount是一个枚举类型,可以设置为LocalService,LocalSystem,NetworkService以及User值。其中,LocalService的安全性最低,User值的安全性最高,需要有效的用户账号方才可以安装服务。
{
//It needs be ran at the command mode
//Type installutil filename to install the windows service
//Type services.msc to access the Service Control Manager(SCM) and browse the windows services
//Type installutil /u filename to uninstall the windows service
[RunInstaller(true)]
public partial class ExplorerServiceInstaller : Installer
{
private ServiceProcessInstaller m_process;
private ServiceInstaller m_service;
public ExplorerServiceInstaller()
{
InitializeComponent();
m_process = new ServiceProcessInstaller();
m_process.Account = ServiceAccount.LocalSystem;
m_service = new ServiceInstaller();
m_service.ServiceName = "DocumentsExplorerService";
Installers.Add(m_process);
Installers.Add(m_service);
}
}
}
对于安装程序而言,也可以直接在设计器视图下设置它的属性。
安装程序直接建立在Windows服务的程序集中,编译之后会获得一个exe文件,例如DocumentsExplorer.exe。然后,我们通过在Visual Studio的Command Prompt模式下运行如下命令:installutil DocumentsExplorer.exe ,即可完成对服务宿主的安装。
打开服务控制管理器(可以在Command Prompt模式下输入Services.msc打开),可以看到名为DocumentsExplorerService的服务:

0
相关文章