设为首页 收藏本站
查看: 1889|回复: 0

VS 2008 打包Windows 服务

[复制链接]

尚未签到

发表于 2015-4-29 12:36:31 | 显示全部楼层 |阅读模式
  1 建立一个控制台程序:
  代码很简单就是在C盘上写一个文本文件。看代码:
  


1 namespace ConsoleApplication
2 {
3     class Program
4     {
5         static void Main(string[] args)
6         {
7             CreateFile();
8         }
9
10         public static void CreateFile()
11         {
12             StreamWriter sw = File.CreateText(@"C:\ThisCreatedByWindowsService.txt");
13             sw.Close();
14         }
15     }
16 }  
  
  2 我们的目的是将 上面的生成的控制台程序 "ConsoleApplication.exe" 打包成一个 Winsdows Service。接下来我们就在建议一个Windows Service 项目。
  我们添加一个名为 TestService.cs的服务类.并在设计界面 点击右键 选择 “Add Install”。
DSC0000.png
  此时,系统会自动生成一个 ProjectInstall.cs类。 下面我们来设置一些属性(直接看图吧,不多说了):
DSC0001.png
DSC0002.png
  我们在这里设置 Account 为 "LocalSystem",因为我懒的管理权限 哈哈..
  下面要复写 ServiceBase 类的 OnStart 方法。(Program 的访问权限得修改一下)
  


1 partial class TestService : ServiceBase
2     {
3         public TestService()
4         {
5             InitializeComponent();
6         }
7
8         protected override void OnStart(string[] args)
9         {
10              ConsoleApplication.Program.CreateFile();
11         }
12
13         protected override void OnStop()
14         {
15              
16         }
17     }  
  好了,现在只差给这个服务打包了。
  3 建立一个 “Setup Project”。添加一个"Project output"。“Project” 选择给那刚才的 Windows Service 项目。
  
DSC0003.png DSC0004.png
  然后添加 "Custom Actions".这里为 Install,Uninstall 添加。这个步骤必须做否则服务不会被安装和卸载。
DSC0005.png
  编译成功后,我们就基本达到了目的。服务被安装上了:
DSC0006.png
  等等,感觉不老好的... 对!没有自动启动呀!要解决这个问题 我们就得对 “ProjectInstaller ”类下手了:
  
  


1 [RunInstaller(true)]
2     public partial class ProjectInstaller : Installer
3     {
4         public ProjectInstaller()
5         {
6             InitializeComponent();
7
8             this.serviceInstaller1.AfterInstall += new InstallEventHandler(serviceInstaller_AfterInstall);
9             this.serviceInstaller1.AfterRollback += new InstallEventHandler(serviceInstaller_AfterRollback);
10             this.serviceInstaller1.AfterUninstall += new InstallEventHandler(serviceInstaller_AfterUninstall);
11
12             this.serviceInstaller1.BeforeInstall += new InstallEventHandler(serviceInstaller_BeforeInstall);
13             this.serviceInstaller1.BeforeRollback += new InstallEventHandler(serviceInstaller_BeforeRollback);
14             this.serviceInstaller1.BeforeUninstall += new InstallEventHandler(serviceInstaller_BeforeUninstall);
15         }
16
17         void serviceInstaller_BeforeRollback(object sender, InstallEventArgs e)
18         {
19             StopService();
20         }
21
22         void serviceInstaller_BeforeInstall(object sender, InstallEventArgs e)
23         {
24             StopService();
25         }
26
27         void serviceInstaller_AfterUninstall(object sender, InstallEventArgs e)
28         {
29             StopService();
30         }
31
32         void serviceInstaller_AfterRollback(object sender, InstallEventArgs e)
33         {
34             StopService();
35         }
36
37         void serviceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
38         {
39             StopService();
40         }
41
42         void serviceInstaller_AfterInstall(object sender, InstallEventArgs e)
43         {
44             StartService();
45         }
46
47         void StartService()
48         {
49             System.ServiceProcess.ServiceController serverContorler = new ServiceController(this.serviceInstaller1.ServiceName);
50
51             if (serverContorler != null)
52             {
53                 if (serverContorler.Status != ServiceControllerStatus.Running)
54                 {
55                     serverContorler.Start();
56                     serverContorler.Dispose();
57                 }
58
59             }
60
61         }
62
63         void StopService()
64         {
65             System.ServiceProcess.ServiceController serverContorler = new ServiceController(this.serviceInstaller1.ServiceName);
66
67             if (serverContorler != null)
68             {
69                 if (serverContorler.CanStop)
70                 {
71                     serverContorler.Stop();
72                     serverContorler.Dispose();
73                 }
74
75             }
76         }
77     }  
  自己启动了! C盘上的文件也建立起来了!
DSC0007.png
  
  补充:
  1. 调试安装程序包:在 ProjectInstaller 类里面你想调试的方法里(例如:OnAfterInstall)添加:
  System.Diagnostics.Debugger.Launch();
  2. 制定 Customer Action:
  选择安装项目->进入"Custom Action"视图-> 以Install为例,选中"Install"->右键 "Add Custom Action"->选择项目输出->在 "CustomActionData"
  中填写 /pwd=[EDITA1] /targetdir="[TARGETDIR]\"  说明:[EDITA1]是自添加的对话框的 textbox edit1的Property “[TARGETDIR]” 是系统参数
  值为安装目录.
  
  在 ProjectInstall类中 获得参数代码:
   InstallContext installContext = this.Context;
  
     string password = installContext.Parameters["pwd"];
  

  源码

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61934-1-1.html 上篇帖子: Biztalk 2009在Windows 2008 R2环境中的High Availability(Cluster群集)部署(下)--AA模式 下篇帖子: 如何在Windows Server 2008中使用Web网站或WebService连接SAP系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表