【发布时间】:2010-10-10 06:10:23
【问题描述】:
构建 Windows 服务最简单的语言是什么?
在这种情况下,最简单的定义是代码量最少,进入语言的最低点。
【问题讨论】:
标签: windows-services
构建 Windows 服务最简单的语言是什么?
在这种情况下,最简单的定义是代码量最少,进入语言的最低点。
【问题讨论】:
标签: windows-services
冒着明显的风险,如果你有任何 C/C++/Java 背景,我认为 C# 为你提供了最低的入门点。
假设您使用的是 Visual Studio 2008,您可以按照以下步骤操作:
false 以防止默认情况下将事件写入应用程序事件日志(注意:我并不是说您不应该记录服务事件;我只是更喜欢写入自己的事件日志而不是应用程序日志 - 见下文)true
true,您还需要覆盖 OnShutdown 方法。我在下面创建了一个示例来说明这些函数的使用。using System.Diagnostics;
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
{
installer.Log = "ServiceExample"; // enter your event log name here
}
}
private EventLogInstaller FindInstaller(InstallerCollection installers)
{
foreach (Installer installer in installers)
{
if (installer is EventLogInstaller)
{
return (EventLogInstaller)installer;
}
EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
if (eventLogInstaller != null)
{
return eventLogInstaller;
}
}
return null;
}
}
此时,您可以构建项目以获取 Windows 服务可执行文件。要安装您的服务,请打开 Visual Studio 2008 命令提示符,然后导航到可执行文件所在的 Debug 或 Release 目录。在命令提示符处,键入以下内容:InstallUtil ServiceExample.exe。这将在本地机器上安装您的服务。要卸载它,请在命令提示符处键入以下内容:InstallUtil /u ServiceExample.exe
只要您的服务没有运行,您就可以对您的服务进行更改并重新构建,也就是说,您不必卸载您的服务来对其进行更改。但是,只要它正在运行,您就无法用您的修复和增强功能覆盖该可执行文件。
要查看您的服务的运行情况,请打开 ServiceExample.cs 文件并进行以下更改:
using System.Diagnostics;
public partial class ServiceExample : ServiceBase
{
public ServiceExample()
{
// Uncomment this line to debug the service.
//Debugger.Break();
InitializeComponent();
// Ties the EventLog member of the ServiceBase base class to the
// ServiceExample event log created when the service was installed.
EventLog.Log = "ServiceExample";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("The service was started successfully.", EventLogEntryType.Information);
}
protected override void OnStop()
{
EventLog.WriteEntry("The service was stopped successfully.", EventLogEntryType.Information);
}
protected override void OnShutdown()
{
EventLog.WriteEntry("The service was shutdown successfully", EventLogEntryType.Information);
}
}
使用这些更改运行服务后,您可以在事件查看器中查看 ServiceExample 事件日志并查看其中记录的消息。
希望这会有所帮助。
编辑:如果您更喜欢使用应用程序事件日志而不是自定义事件日志记录,只需不更改 ProjectInstaller.cs 文件。此外,省略在 ServiceExample 构造函数中设置 EventLog 的 Log 属性的行。当您运行服务时,您的日志消息将出现在应用程序事件日志中。
【讨论】:
System.UnauthorizedAccessException: Access to the path 'C:\MyService.InstallState' is denied. 原来你必须运行 cmd.exe:以管理员身份运行。
我同意所有在其他地方做出回应的人的观点,但我想说不要过多关注实际语言,只要您在 .NET 框架中工作,并且有一个适合您的入门项目,您就可以了。重新出发。我过去做过几个“Windows 服务”,并且用 VB.NET 和 C# 都用最少的代码开发了它们。
我建议 OP 做的是学习如何为其构建安装程序包。安装该服务就像执行“installutil.exe {drive}\path\to\file.exe”一样简单,但是当您必须做比部署“hello world”Windows 服务更大的事情时,最好了解并了解如何以有意义的方式部署服务。
不要引发一场激烈的战争,但我已经“标准化”了在我所有的部署包中使用WiX,除了做老式的 COM 互操作类型的东西,因为要正确安装这是一项首当其冲的工作.我渴望 WiX 团队开发引导程序,允许您将先决条件和主 msi 放入可以下载的 EXE 文件中。他们预定 3.5 版本,所以我会耐心等待,暂时使用 WinZip 自解压可执行文件。
【讨论】:
对我来说,我只尝试了几种方法,Visual Studio 和 C# 是最简单的。
Visual Studio 为框架完成了所有必要的服务管道设置,我发现 C# 非常容易学习,从 VB6、VB.NET 和 C 迁移而来。
【讨论】:
使用 Visual C#,您将在网上找到最多的代码示例。 与 Visual Studio 结合使用,只需在公园中散步即可启动并运行基本的 Windows 服务。 Visual Studio 还可以轻松创建 MSI 安装程序包。
那是我的选择
【讨论】:
C# 中的 Windows 服务项目将通过单击按钮为您提供来自 Visual Studio 模板的完全可部署的服务。您只需要添加您的有效负载代码。
【讨论】:
使用 Visual Studio Service 类型的项目,使用 C# 或 VB.NET。
我个人更喜欢 C#,但总的来说它很容易理解生命周期并在所需阶段编写逻辑代码。
构建安装程序也很容易。
【讨论】: