我正在编写一个简单的Windows服务来学习。这是我的项目结构:
WindowsService.cs
using System.ServiceProcess;
using System.Diagnostics;
namespace ServiceTest
{
class WindowsService : ServiceBase
{
/// <summary>
/// Public Constructor for WindowsService.
/// - Put all of your Initialization code here.
/// </summary>
EventLog serviceEventLog;
public WindowsService()
{
// Configure service event logging
serviceEventLog = new EventLog();
this.AutoLog = false;
if (!EventLog.SourceExists("MyApp"))
{
EventLog.CreateEventSource("MyApp", "ServiceTest");
}
serviceEventLog.Source = "MyApp";
serviceEventLog.Log = "ServiceTest";
// Configure service properties
this.ServiceName = "My Windows Service";
this.EventLog.Log = "ServiceTest";
// List of events this service can handle
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
}
/// <summary>
/// The Main Thread: This is where your Service is Run.
/// </summary>
static void Main()
{
ServiceBase.Run(new WindowsService());
}
/// <summary>
/// Dispose of objects that need it here.
/// </summary>
/// <param name="disposing">Whether
/// or not disposing is going on.</param>
protected override void Dispose(bool disposing)
{
// Custom code
// Run parent code
base.Dispose(disposing);
}
/// <summary>
/// OnStart(): Put startup code here
/// - Start threads, get inital data, etc.
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
serviceEventLog.WriteEntry("Service Started");
base.OnStart(args);
}
/// <summary>
/// OnStop(): Put your stop code here
/// - Stop threads, set final data, etc.
/// </summary>
protected override void OnStop()
{
serviceEventLog.WriteEntry("Service Stopped");
base.OnStop();
}
/// <summary>
/// OnPause: Put your pause code here
/// - Pause working threads, etc.
/// </summary>
protected override void OnPause()
{
serviceEventLog.WriteEntry("Service Paused");
base.OnPause();
}
/// <summary>
/// OnContinue(): Put your continue code here
/// - Un-pause working threads, etc.
/// </summary>
protected override void OnContinue()
{
serviceEventLog.WriteEntry("Service Resumed");
base.OnContinue();
}
/// <summary>
/// OnShutdown(): Called when the System is shutting down
/// - Put code here when you need special handling
/// of code that deals with a system shutdown, such
/// as saving special data before shutdown.
/// </summary>
protected override void OnShutdown()
{
serviceEventLog.WriteEntry("Service Shutdown");
base.OnShutdown();
}
}
}
WindowsServiceInstaller.cs
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace ServiceTest
{
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
/// <summary>
/// Public Constructor for WindowsServiceInstaller.
/// - Put all of your Initialization code here.
/// </summary>
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "My New C# Windows Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "My Windows Service";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}
INSTALL.BAT
@ECHO OFF
REM The following directory is for .NET 4.0
set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX4%
echo Installing ServiceTest...
echo ---------------------------------------------------
InstallUtil /i "%~dp0ServiceTest.exe"
echo ---------------------------------------------------
echo Done.
@PAUSE
我将install.bat
脚本作为Administrator
运行并安装了服务。当我点击开始时,它一直停留在开始:
如果我右键单击该服务并单击Stop
,我会收到以下信息:
我在使用.NET Framework 4.5的Windows 10上。任何想法有什么不对?