lewlee的博客

lewlee的博客

导航

鼓起勇气开博了

之前在园子里经常看高手们的博文,感叹他们知识之扎实,技术之精湛。

于是今天终于开博,很激动,当然不是想成为他们一样。成为牛人,高手。而是时而会回过头来想想,之前学过的很多知识,虽然都有印象,但缺乏有效的实践。

事实证明,多实践才是对自己所学知识的提高。

闲话不扯,记录下今天遇到的问题:

今天练习在使用windows服务来作为WCF的宿主服务(注:此工程很久之前就已经建立过,可能动过了,这就是没有做笔记的坏习惯呀)。步骤如下:

1.通过文件->新建->项目->Windows服务,新建Windows服务项目

2.vs自动生成Program.cs文件及ProjectInstaller服务组件

3.通过自带的installutil工具,自动生成服务

可在第3点的时候出了问题如下:

Setting environment for using Microsoft Visual Studio 2010 x86 tools.

D:\Program Files\Microsoft Visual Studio 10.0\VC>installutil D:\winform\Vs2010\W
CFDemo\Host\bin\Debug\WCFDemo.Host.exe
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.


Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the D:\winform\Vs2010\WCFDemo\Host\bin\Debu
g\WCFDemo.Host.exe assembly's progress.
The file is located at D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.Ins
tallLog.
Installing assembly 'D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.exe'.

Affected parameters are:
   logtoconsole =
   logfile = D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.InstallLog
   assemblypath = D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.exe

The Install phase completed successfully, and the Commit phase is beginning.
See the contents of the log file for the D:\winform\Vs2010\WCFDemo\Host\bin\Debu
g\WCFDemo.Host.exe assembly's progress.
The file is located at D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.Ins
tallLog.
Committing assembly 'D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.exe'.

Affected parameters are:
   logtoconsole =
   logfile = D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.InstallLog
   assemblypath = D:\winform\Vs2010\WCFDemo\Host\bin\Debug\WCFDemo.Host.exe

The Commit phase completed successfully.

The transacted install has completed.

D:\Program Files\Microsoft Visual Studio 10.0\VC>

 

大概看了下,貌似成功了,可到服务里面查看了下,也没有找到这个服务。认为这个命令没有成功;

于是在重新新建Windows服务,测试通过。

后来经过对比文件,原来是因为没有把当前的两个组件(ServiceInstaller与ProcessInstaller)添加到Installers集合中。

查阅相关资料,在创建Windows服务时一般需要注意一下几个点:

第一:在创建服务组件时,生成安装文件时会创建两个Installer组件

第二:服务组建继承于System.ServiceProcess.ServiceBase

第三:processInstall.Account用于设置或获取此服务运行的账户

第四:serviceInstall.DisplayName/ServiceName,用于显示服务名/唯一标识此服务的名称

第五:serviceInstall.StartType服务启动的类型:一般有自动,手工,禁用

代码如下:

 

/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.processInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
            // 
            // processInstaller
            // 
            this.processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
            
            
            //this.processInstaller.a
            // 
            // serviceInstaller
            // 
            this.serviceInstaller.DisplayName = "WCFDemoServerService";
            this.serviceInstaller.ServiceName = "WCFDemoServerService";
            this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.processInstaller,
            this.serviceInstaller
            
            });
        }

 

posted on 2012-01-03 23:03  lewlee  阅读(435)  评论(2)    收藏  举报