windows服务安装部署

之前没有做过安装盘,一直在做Web开发,基本上不用安装部署,只要用VS2005发布程序就OK了

这次我们的站点用到一个服务,测试那边正好忙,负责测试的同事不会部署,改配置有点麻烦~

VS2005的安装部署也挺简单的~主要安装部署服务以及改写配置可能有点麻烦

不过做过一次安装部署的话,肯定会觉得也是很简单的了~

 

 

下面是对已开发好的服务安装部署:

1.在服务的文件上右键查看设计器,打开服务文件设计视图

  在设计视图上点右键,添加“安装程序”保存为ProjectInstaller.cs

  设置serviceProcessInstaller的Account为LocalSystem

  设置serviceInstaller的StartType为Automatic

 

2.然后添加安装部署项目(添加项目和服务相关的项目为主输出,添加服务为内容输出项) 


3.选择安装部署项目的“自定义操作编辑器”

  在安装文件夹上添加自定义操作,选择应用程序->主输出->服务所在的项目,确认

 

在卸载文件夹上添加自定义操作,选择应用程序->主输出->服务所在的项目,确认;设置Arguments参数为/u


4.如果要让安装过程中来完成配置的修改

  则需要安装项目->用户界面里面添加文本框,要属性里设置各文本框的property属性以及label(提示)

  再回到自定义操作编辑器,在安装文件夹中添加的主输出的属性设置CustomActionData项的值/dbsource=[DBSOURCE] /dbuser=[DBUSER] /dbpassword=[DBPASSWORD] /sleeptime=[SLEEPTIME]

5.打开ProjectInstaller.cs的代码,写一个继承基类Install的方法

 

 1 public override void Install(System.Collections.IDictionary stateSaver)
 2         {
 3             base.Install(stateSaver);            
 4 
 5             string dbSource = this.Context.Parameters["dbsource"];
 6             string dbuser = this.Context.Parameters["dbuser"];
 7             string dbpassword = this.Context.Parameters["dbpassword"];            
 8             string sleepTime = this.Context.Parameters["sleeptime"];
 9             AppSettings.path = this.Context.Parameters["assemblypath"];
10 
11             string connStr = "Data Source=" + dbSource + ";user=" + dbuser + ";password=" + dbpassword + ";";
12 
13             AppSettings.SetDBConnectionValue(connStr);
14             AppSettings.SetSleepTimeValue(sleepTime);            
15             
16         }

 

this.Context.Parameters["assemblypath"] 为安装后应用程序的路径(在保存app.config时有用)


install方法中用到的AppSettings类如下:

 

 1 public class AppSettings
 2     {
 3         public static string path = string.Empty;
 4         //读取Value值 
 5         public static string GetConfigString(string key)
 6         {
 7             return System.Configuration.ConfigurationManager.AppSettings[key];
 8         }
 9         //写操作 
10         protected static void SetValue(string AppKey, string AppValue)
11         {
12             try
13             {
14                 XmlDocument xDoc = new XmlDocument();
15                 //获取可执行文件的路径和名称 
16                 xDoc.Load(path + ".config");
17 
18                 XmlNode xNode;
19                 XmlElement xElem1;
20                 XmlElement xElem2;
21                 xNode = xDoc.SelectSingleNode("//appSettings");
22 
23                 xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
24                 if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
25                 else
26                 {
27                     xElem2 = xDoc.CreateElement("add");
28                     xElem2.SetAttribute("key", AppKey);
29                     xElem2.SetAttribute("value", AppValue);
30                     xNode.AppendChild(xElem2);
31                 }
32                 xDoc.Save(path + ".config");
33                 
34             }
35             catch (Exception Ex)
36             {
37                 Log.WriteLog("----------安装配置出错!----------",Ex);                
38             }
39         }
40 
41         public static void SetDBConnectionValue(string strValue)
42         {           
43             SetValue("ConnectionString", strValue);           
44         }
45 
46         public static void SetSleepTimeValue(string strValue)
47         {
48             SetValue("SleepTime", strValue);
49         }

 

posted @ 2008-11-06 18:56  bino  阅读(598)  评论(0编辑  收藏  举报