在“视图”菜单中单击“代码”以打开代码编辑器。编辑 Main 方法以创建“MyNewService”的实例。当在步骤 3 中重命名服务时,未在 Main 方法中修改类名。在 Visual C# 和 Visual J# 应用程序中,Main 方法分别位于 Program.cs 和 Program.js 文件中。
Visual Basic 复制代码
' To access the Main method in Visual Basic, select Main from the
' method name drop-down list. This expands the Component Designer
' generated code region.
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' Change the following line to match.
ServicesToRun = New System.ServiceProcess.ServiceBase() _
{New MyNewService()}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
C# 复制代码
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
J# 复制代码
public static void main(String[] args)
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
向服务添加功能
在下一节中,将自定义事件日志添加到 Windows 服务中。事件日志与 Windows 服务没有任何形式的关联。此处,EventLog 组件用作可以添加到 Windows 服务的组件类型的示例。有关自定义事件日志的更多信息,请参见如何:创建和移除自定义事件日志。
Visual Basic 复制代码
' To access the constructor in Visual Basic, select New from the
' method name drop-down list.
Public Sub New()
MyBase.New()
InitializeComponent()
If Not System.Diagnostics.EventLog.SourceExists("MySource") Then
System.Diagnostics.EventLog.CreateEventSource("MySource", _
"MyNewLog")
End If
EventLog1.Source = "MySource"
EventLog1.Log = "MyNewLog"
End Sub
C# 复制代码
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource","MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
J# 复制代码
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.set_Source("MySource");
eventLog1.set_Log("MyNewLog");
}
Visual Basic 复制代码
' To access the OnStart in Visual Basic, select OnStart from the
' method name drop-down list.
Protected Overrides Sub OnStart(ByVal args() As String)
EventLog1.WriteEntry("In OnStart")
End Sub
生成项目后,便可以对其进行部署了。安装项目将安装已编译的项目文件并运行要运行 Windows 服务所需的安装程序。若要创建完整的安装项目,您必须将项目输出“MyNewService.exe”添加到安装项目,然后添加自定义操作来安装“MyNewService.exe”。有关安装项目的更多信息,请参见安装项目。有关自定义操作的更多信息,请参见演练:创建自定义操作。
在“视图”菜单中单击“代码”以打开代码编辑器。编辑 Main 方法以创建“MyNewService”的实例。当在步骤 3 中重命名服务时,未在 Main 方法中修改类名。在 Visual C# 和 Visual J# 应用程序中,Main 方法分别位于 Program.cs 和 Program.js 文件中。
Visual Basic 复制代码
' To access the Main method in Visual Basic, select Main from the
' method name drop-down list. This expands the Component Designer
' generated code region.
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' Change the following line to match.
ServicesToRun = New System.ServiceProcess.ServiceBase() _
{New MyNewService()}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
C# 复制代码
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
J# 复制代码
public static void main(String[] args)
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
向服务添加功能
在下一节中,将自定义事件日志添加到 Windows 服务中。事件日志与 Windows 服务没有任何形式的关联。此处,EventLog 组件用作可以添加到 Windows 服务的组件类型的示例。有关自定义事件日志的更多信息,请参见如何:创建和移除自定义事件日志。
Visual Basic 复制代码
' To access the constructor in Visual Basic, select New from the
' method name drop-down list.
Public Sub New()
MyBase.New()
InitializeComponent()
If Not System.Diagnostics.EventLog.SourceExists("MySource") Then
System.Diagnostics.EventLog.CreateEventSource("MySource", _
"MyNewLog")
End If
EventLog1.Source = "MySource"
EventLog1.Log = "MyNewLog"
End Sub
C# 复制代码
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource","MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
J# 复制代码
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.set_Source("MySource");
eventLog1.set_Log("MyNewLog");
}
Visual Basic 复制代码
' To access the OnStart in Visual Basic, select OnStart from the
' method name drop-down list.
Protected Overrides Sub OnStart(ByVal args() As String)
EventLog1.WriteEntry("In OnStart")
End Sub
生成项目后,便可以对其进行部署了。安装项目将安装已编译的项目文件并运行要运行 Windows 服务所需的安装程序。若要创建完整的安装项目,您必须将项目输出“MyNewService.exe”添加到安装项目,然后添加自定义操作来安装“MyNewService.exe”。有关安装项目的更多信息,请参见安装项目。有关自定义操作的更多信息,请参见演练:创建自定义操作。