惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - YFeng_Lee

webHttpBinding+wsHttpBinding+basicHttpBinding的区别 (转) AppDomain对于静态对象的独享引用 AppDomain 详解(转) .net工具 InstallShield12 添加自定义对话框,并根据输入值修改对应的Config文件。 普通管理类程序开发之难度系数、层次之说法(转自csdn) 待阅读书目 C#开发Windows Service程序 Ext简介(转) Ext的组件结构分析(转) XML与数据库 分析模式 - 责任模式 NHibernate Cascades: the different between all, all-delete-orphans and save-update 企业开发框架NHibernate和Spring.Net简介-4 企业开发框架NHibernate和Spring.Net简介-3 企业开发框架NHibernate和Spring.Net简介-2 企业开发框架NHibernate和Spring.Net简介-1 Nhibernate学习之many-to-many篇(转 明了篇) Nhibernate学习起步之many-to-one篇(转 明了篇)
创建一个windows service应用程序
YFeng_Lee · 2009-11-27 · via 博客园 - YFeng_Lee

正是由于.net framework的出现,才使你能够构建出在系统重新启动时自动运行的、无人参与的(unattended)应用程序。
by stan schultes
技术工具箱:vb.net, xml, asp
windows service应用程序曾经是c++程序员专用的领域,除非你在vb中使用了第三方工具。现在它们则成为system.serviceprocess 命名空间下的.net framework类库中的主要部分,你可以随意使用任何.net语言来构建它。windows service是一种系统自动的、无人参与的程序(仅存在于windows nt、2000和xp操作系统中),它能够在系统启动时开始运行。你可以通过service control manager (scm) applet或者一些特殊的service-control应用(utility)来访问windows service。

我将讲解如何构建一个用于监控文件改变情况的windows service。filechangemonitor service用于随意地编写事务日志(event-log)条目以及当文件在一段时间内没有改变时发送e-mail。这种文件监控过程在用于确保需要时进行备份、正常运转报告生成器或按时间表将文件传送到远程系统上时是非常有用的。filechangemonitor service还能够发送显示程序正常运行的综合报告。

第一步是构建一个作为将来service项目起始点的windows service程序的模板。打开visual studio.net,用windows service模板来创建一个新的项目,将其命名为filechangemonitor(点此下载范例代码)。鼠标右键单击solution explorer(se)中的service1.vb文件并将其重新命名为changemonitor.vb。点击changemonitor 设计界面(你会看到“to add components to your class”消息),并将其在属性窗口(按f4显示的窗口)中的的名字和servicename属性均改为changemonitor。

同样将属性窗口中的canpauseandcontinue和canshutdown属性值设置为true。 这些属性控制着该service程序是否能够暂停/继续,以及在系统关闭时是否做出响应。你将在后面使用这些事件(以及stop事件)来保存你的service的“状态”――即给定时间内的执行文本(execution context )。

接下来,点击changemonitor 设计窗口中的“click here to switch to code view”链接。在代码窗口中,点击左边的加号来打开名为“component designer generated code”的区域。在sub main过程(routine)中,将servicestorun 赋值语句中的service1改为changemonitor: servicestorun = new system. _
   serviceprocess.servicebase() _
   {new changemonitor()}

右键单击在se中的filechangemonitor项目,选中属性,再从startupobject下拉列表中选择sub main。现在你就可以开始构建你的项目了(通过使用build | build solution菜单项)。

创建事件模板和过程
现在,在模板中加入一些事件过程(event-routine)的原型。你等一会儿可以将代码添加到这些service事件中去以便处理 windows service程序中的状态变化。在代码窗口中点击class name combo box(位于代码窗口上面左侧的combo box),然后选中(overrides)选项。在method name combo box中(位于代码窗口上面右侧的combo box),依次选中各项以便将过程原型(一个空程序)添加到代码窗口中,比如:oncontinue、onpause和onshutdown。你必须在每个过程中的method name combo box中重新选择(overrides)选项 。

接下来就开始构建过程模板。在你需要添加代码的changemonitor.vb中创建五个子过程原型: loadsettings()、savesettings()、runcheck()、runsummary()和startservice()。在这个类的最顶部imports system.serviceprocess语句的下面,通过使用imports语句来添加其他需要用到的命名空间:imports system.io
imports system.timers
imports system.web.mail
imports system.reflection
imports system.xml.serialization

由于你的service应用程序不是一个web项目,因此你可能需要手动将一个引用(reference)添加到web.mail命名空间下。右键单击se中的filechangemonitor项目,从弹出菜单中选择add reference。在 add reference对话框中选择列表中的system.web.dll条目,单击select按钮,然后点ok。

在changemonitor类的顶部、component designer区域的前面,添加一个用于文件检查功能的timer对象的声明: private withevents controltimer as timer

然后将下面三行代码加到onstart和oncontinue事件程序中: startservice()
runcheck()
runsummary()

当你的service启动时会触发onstart事件,而当暂停后继续运行时则会触发oncontinue事件。

在class name combo box中选择controltimer,并在method name combo box中选择elapsed。这样就会将controltimer_elapsed事件过程原型添加到项目中了。只需添加这两个run语句到controltimer_elapsed事件过程中,然后在onpause、onshutdown和onstop事件过程中添加代码来中断计时器(timer)并保存设置: controltimer.stop()
savesettings()

从这个简单的程序大纲中你可以看到用于windows service应用程序的文件检查功能的控制流程是如何运作的。当其中一个事件触发时,startservice函数便会加载设置,然后运行文件和简要检查。你可以在计时器时间到时运行检查程序;可以用stop或pause事件来中断计时器以及保存设置。

在startservice过程中添加代码来创建计时器,将时间间隔定为15秒(以毫秒值计),然后启动它:controltimer = new timer()
controltimer.interval = 15000
controltimer.autoreset = true
controltimer.start()

autoreset = true属性设定使计时器在时间到时继续运行。接下来,你可以在check函数运行时添加代码来编写windows application事务日志,这样你就可以看到service正在运行: private sub runcheck()
   eventlog.writeentry(servicename & _
      " - check", "checking files.")
end sub

你可以通过windows中的event viewer(ev)应用程序来查看事件日志中的消息。

添加一个安装程序(installer)
创建service应用程序模板的最后一项工作是在项目中添加一个安装程序。在它运行之前你需要首先注册这个windows service程序。切换到changemonitor设计界面并打开属性窗口(如果看不到的话可以按f4),你可以看到在属性窗口下面有一个名为add installer的链接,点击该链接后会出现一个向导来引导你将一个名为projectinstaller的组件添加到当前项目中。这个向导在projectinstaller设计界面中放置了两个service控件:serviceprocessinstaller和serviceinstaller。

图1. 设置service属性
点击serviceprocessinstaller控件并对account属性进行设置。你可能希望选择localsystem(大多数service是运行在localsystem中的),但如果你喜欢,你还可以将它设置为一个用户帐户。点击serviceinstaller,将它的属性设置为displayname = filechangemonitor、 servicename = changemonitor以及starttype = automatic(如果你希望通过手动启动该service的话也可以将它设置为manual)。

通过build | build solution来构建你自己的项目,确保不存在什么错误。现在你就可以使用一个名为installutil的framework应用来安装你的service了。打开命令行提示(command-prompt)窗口并执行corvars.bat文件来设置环境变量(你可以下载readme文件来了解详细内容)。使用cd(change directory)命令将其导航到你项目中的\bin目录下。执行以下命令来安装你的service:> installutil filechangemonitor.exe

现在你可以使用control panel的administrative tools菜单(win2k 和winxp操作系统)中的scm applet来启动、停止、暂停以及继续运行你的filechangemonitor service了 (见图1)。当你启动这个service后就可以使用ev来查看application事务日志中由该service生成的事件了。点击f5刷新ev的显示,你会看到“check messages every 15 seconds”。你可以使用以下命令来卸载这个service(先用scm来终止service): > installutil filechangemonitor.exe /u

现在你可以保存该项目并将它作为以后service程序的一个模板。

service以某种状态运行(这可能是它未运行时保存的一组设定)。当service重新启动时,你可以加载它上次运行时的状态。一个简便的方法是使用.net framework中的序列化类(serialization class),如system.xml.serialization命名空间下的xml序列化(你也可以选择binary和soap序列化)。

用xml序列化保存设置
xml序列化是和用<serializable()>属性标记的类配合使用的。右键单击se中的filechangemonitor项目,从弹出的菜单中选择add | add class,将该类命名为cmonitor并点击 ok。在cmonitor中添加两个类――monitorheader和monitorfile: <serializable()> public class monitorheader
   public monitorintsecs as integer
   <other header properties>
   public files() as monitorfile
end class
<serializable()> public class monitorfile
   public path as string
   <other file properties>
end class

monitorheader包含控制service的设置,包括一组monitorfile对象。monitorfile对象中包含每个你想要检查进度的文件的监控设置。你可以将大多数类的属性当作public变量来实现,因为它们只用于你的service项目中。查看范例代码来了解该类的完整定义。你可以在changemonitor 类的模块中实现loadsettings和savesettings序列化过程(见列表1)。

在用于声明设置对象和存储设置文件路径的changemonitor类的顶部添加该类的私有变量:private m_monitorcontrol as new _
   monitorheader()
private m_ssettingspath as string

在调用startservice()之前将代码添加到onstart过程中,来完成在service启动之前对设置文件名进行检测。用reflection来找到应用程序的runtime .exe路径,并用substitute .xml作为文件的扩展名(这两个文件在同一目录下): m_ssettingspath = [assembly]. _
   getentryassembly.location. _
   replace(".exe", ".xml")

在vb.net中assembly是一个关键字,因此在代码中你必须把它用一个方括号括起来。你需要整理startservice()过程以便从设置文件中加载计时器间隔: if m_monitorcontrol _
   .monitorintervalsecs > 0 then
   controltimer.interval = _
      ctype(m_monitorcontrol _
      .monitorintervalsecs * 1000, double)
   controltimer.autoreset = true
   controltimer.start()
end if

现在,你可以实现主要的检查函数――runcheck了(见列表2)。runcheck负责调用runalarmaction并计算出files数组中每个文件的出现次数,该文件的nextcheck代表的是当前时间之前的时间并且自从上次检查以后就没有发生过改变。runalarmaction负责检查alarmaction标记、编写事务日志,然后通过sendemail过程来发送e-mail (见列表3)。

你可以采用类似的方法,基于monitorheader结构的汇总设置(summary setting)来实现runsummary和runsummaryaction方法,构建并测试你的windows service应用程序。范例代码中还包括一个名为filechgctl的service-control应用。它主要用于调试目的,可以将自定义命令(custom command)发送到filechangemonitor service中。自定义命令是一个范围在128到255之间的整数。当windows service应用程序中的oncustomcommand事件触发时你会得到自定义命令出现的通知。

你会发现service 应用程序在很多情况下是非常有用的,比如用在系统维护、监控以及其他自动的、无人照顾的操作中。windows service通常会通过事务日志来记录其活动情况,但正如我所介绍的,你的service也可以通过发送e-mail来方便地跟踪其进展情况。构建windows service应用程序的能力使你在用windows构建有效的商务方案方面得以轻松地扩展。