ServiceBase 類別

定義

提供將會屬於服務應用程之服務的基底類別。Provides a base class for a service that will exist as part of a service application. 當建立新的服務類別時,必須衍生自 ServiceBaseServiceBase must be derived from when creating a new service class.

public ref class ServiceBase : System::ComponentModel::Component
public class ServiceBase : System.ComponentModel.Component
type ServiceBase = class
    inherit Component
Public Class ServiceBase
Inherits Component
繼承
衍生

備註

在服務ServiceBase應用程式中定義您的服務類別時,衍生自。Derive from ServiceBase when defining your service class in a service application. 任何有用的OnStart服務都會覆OnStop寫和方法。Any useful service overrides the OnStart and OnStop methods. 如需其他功能,您可以OnPause使用OnContinue特定行為來覆寫和,以回應服務狀態的變更。For additional functionality, you can override OnPause and OnContinue with specific behavior in response to changes in the service state.

服務是長時間執行的可執行檔,不支援使用者介面,而且可能不會在登入的使用者帳戶下執行。A service is a long-running executable that does not support a user interface, and which might not run under the logged-on user account. 服務可以在沒有任何使用者登入電腦的情況下執行。The service can run without any user being logged on to the computer.

根據預設,服務會在系統帳戶下執行,這與管理員帳戶不同。By default, services run under the System account, which is not the same as the Administrator account. 您無法變更系統帳戶的許可權。You cannot change the rights of the System account. 或者,您可以使用ServiceProcessInstaller來指定將執行服務的使用者帳戶。Alternatively, you can use a ServiceProcessInstaller to specify a user account under which the service will run.

可執行檔可以包含一個以上的服務,但每個ServiceInstaller服務都必須包含個別的。An executable can contain more than one service but must contain a separate ServiceInstaller for each service. ServiceInstaller實例會向系統註冊服務。The ServiceInstaller instance registers the service with the system. 安裝程式也會將每個服務與可用來記錄服務命令的事件記錄檔產生關聯。The installer also associates each service with an event log that you can use to record service commands. main()執行檔中的函式會定義應該執行的服務。The main() function in the executable defines which services should run. 服務目前的工作目錄是系統目錄,而不是可執行檔所在的目錄。The current working directory of the service is the system directory, not the directory in which the executable is located.

當您啟動服務時,系統會尋找可執行檔,並OnStart執行該服務的方法(包含在可執行檔中)。When you start a service, the system locates the executable and runs the OnStart method for that service, contained within the executable. 不過,執行此服務與執行可執行檔的方式不同。However, running the service is not the same as running the executable. 可執行檔只會載入服務。The executable only loads the service. 服務會透過服務控制管理員來存取(例如,已啟動和已停止)。The service is accessed (for example, started and stopped) through the Service Control Manager.

當您第一ServiceBase次在服務上呼叫 Start 時,可執行檔會呼叫衍生類別的函式。The executable calls the ServiceBase derived class's constructor the first time you call Start on the service. 在執行此函式之後,會立即呼叫命令處理方法。OnStartThe OnStart command-handling method is called immediately after the constructor executes. 第一次載入服務之後,不會再次執行此函式,因此您必須將由執行的處理常式所執行的處理作業與所執行OnStart的作業分開。The constructor is not executed again after the first time the service has been loaded, so it is necessary to separate the processing performed by the constructor from that performed by OnStart. 任何可由發行的OnStop資源都應該在中OnStart建立。Any resources that can be released by OnStop should be created in OnStart. 如果在釋放資源之後OnStop再次啟動服務,在此函式中建立資源,會使其無法正確建立。Creating resources in the constructor prevents them from being created properly if the service is started again after OnStop has released the resources.

服務控制管理員(SCM)提供與服務互動的方式。The Service Control Manager (SCM) provides a way to interact with the service. 您可以使用 SCM 將啟動、停止、暫停、繼續或自訂命令傳遞至服務。You can use the SCM to pass Start, Stop, Pause, Continue, or custom commands into the service. SCM 會使用CanStopCanPauseAndContinue的值來判斷服務是否接受停止、暫停或繼續命令。The SCM uses the values of CanStop and CanPauseAndContinue to determine whether the service accepts Stop, Pause, or Continue commands. 只有當對應的屬性CanStopCanPauseAndContinue在服務類別中時true ,才會在 SCM 的內容功能表中啟用 [停止]、[暫停] 和 [繼續]。Stop, Pause, and Continue are enabled in the SCM's context menus only if the corresponding property CanStop or CanPauseAndContinue is true in the service class. 如果已啟用,則會將命令傳遞至服務, OnStopOnPause呼叫、 OnContinue或。If enabled, the command is passed to the service, and OnStop, OnPause, or OnContinue is called. 如果CanStopCanShutdown或為,false則即使您已經執行方法,也不會OnStop處理對應的命令處理方法(例如)。 CanPauseAndContinueIf CanStop, CanShutdown, or CanPauseAndContinue is false, the corresponding command-handling method (such as OnStop) will not be processed, even if you have implemented the method.

您可以使用ServiceController類別,以程式設計方式執行 SCM 使用使用者介面的功能。You can use the ServiceController class to do programmatically what the SCM does using a user interface. 您可以將主控台中的可用工作自動化。You can automate the tasks available in the console. 如果CanStopCanShutdown或為,true但您尚未執行對應的命令處理方法(例如OnStop),則系統會擲回例外狀況並忽略命令。 CanPauseAndContinueIf CanStop, CanShutdown, or CanPauseAndContinue is true but you have not implemented a corresponding command-handling method (such as OnStop) the system throws an exception and ignores the command.

您不需要在中OnStart ServiceBase執行、 OnStop或任何其他方法。You do not have to implement OnStart, OnStop, or any other method in ServiceBase. 不過,中OnStart會描述服務的行為,因此至少應該覆寫這個成員。However, the service's behavior is described in OnStart, so at minimum, this member should be overridden. 可執行檔的Run 功能會藉由呼叫方法,在可執行檔中向服務控制管理員註冊服務。main()The main() function of the executable registers the service in the executable with the Service Control Manager by calling the Run method. 傳遞至ServiceName ServiceBase ServiceName方法之物件的屬性必須符合該服務之服務安裝程式的屬性。RunThe ServiceName property of the ServiceBase object passed to the Run method must match the ServiceName property of the service installer for that service.

您可以使用InstallUtil.exe在系統上安裝服務。You can use InstallUtil.exe to install services on your system.

注意

您可以指定應用程式事件記錄檔以外的記錄檔,以接收服務呼叫的通知,但AutoLog EventLog和屬性都不能寫入自訂記錄檔。You can specify a log other than the Application event log to receive notification of service calls, but neither the AutoLog nor the EventLog property can write to a custom log. 如果AutoLogfalse不想要使用自動記錄,請將設定為。Set AutoLog to false if you do not want to use automatic logging.

建構函式

ServiceBase()

建立 ServiceBase 類別的新執行個體。Creates a new instance of the ServiceBase class.

欄位

MaxNameLength

指示服務名稱的最大值。Indicates the maximum size for a service name.

屬性

AutoLog

指示是否在事件記錄檔中報告啟動、停止、暫停和繼續等命令。Indicates whether to report Start, Stop, Pause, and Continue commands in the event log.

CanHandlePowerEvent

取得或設定值,指出服務是否可以處理電腦電源狀態變更時發生的告知。Gets or sets a value indicating whether the service can handle notifications of computer power status changes.

CanHandleSessionChangeEvent

取得或設定值,指出服務是否可以處理從 Terminal Server 工作階段所擷取的工作階段變更事件。Gets or sets a value that indicates whether the service can handle session change events received from a Terminal Server session.

CanPauseAndContinue

取得或設定值,指出服務是否可以暫停和繼續。Gets or sets a value indicating whether the service can be paused and resumed.

CanRaiseEvents

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
CanShutdown

取得或設定值,指出當系統關閉時是否應該通知服務。Gets or sets a value indicating whether the service should be notified when the system is shutting down.

CanStop

取得或設定值,指出一旦服務已經啟動是否可以停止它。Gets or sets a value indicating whether the service can be stopped once it has started.

Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
EventLog

取得事件記錄檔,您可以利用該事件記錄將服務命令呼叫 (例如啟動和停止) 通知寫入應用程式事件記錄檔。Gets an event log you can use to write notification of service command calls, such as Start and Stop, to the Application event log.

Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
ExitCode

取得或設定服務的結束代碼 (Exit Code)。Gets or sets the exit code for the service.

ServiceHandle

取得服務的服務控制處理。Gets the service control handle for the service.

ServiceName

取得或設定用來識別系統服務的簡短名稱。Gets or sets the short name used to identify the service to the system.

Site

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

處置 (Dispose) ServiceBase 所使用的資源 (除了記憶體之外)。Disposes of the resources (other than memory) used by the ServiceBase.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
OnContinue()

在衍生類別中實作時,當服務控制管理員 (SCM) 傳送繼續命令給服務時,將執行 OnContinue()When implemented in a derived class, OnContinue() runs when a Continue command is sent to the service by the Service Control Manager (SCM). 指定暫停服務後要繼續正常運作所要執行的動作。Specifies actions to take when a service resumes normal functioning after being paused.

OnCustomCommand(Int32)

在衍生類別中實作時,當服務控制管理員 (SCM) 傳遞自訂命令給服務時,將執行 OnCustomCommand(Int32)When implemented in a derived class, OnCustomCommand(Int32) executes when the Service Control Manager (SCM) passes a custom command to the service. 指定在具有指定參數值的命令發生時所要執行的動作。Specifies actions to take when a command with the specified parameter value occurs.

OnPause()

在衍生類別中實作時,當服務控制管理員 (SCM) 傳送暫停命令至服務時執行。When implemented in a derived class, executes when a Pause command is sent to the service by the Service Control Manager (SCM). 指定在服務暫停時所要執行的動作。Specifies actions to take when a service pauses.

OnPowerEvent(PowerBroadcastStatus)

在衍生類別中實作時,當電腦的電源狀態已經變更時執行。When implemented in a derived class, executes when the computer's power status has changed. 這適用於攜帶型電腦,當它們進入暫停模式的時候,不同於系統關閉。This applies to laptop computers when they go into suspended mode, which is not the same as a system shutdown.

OnSessionChange(SessionChangeDescription)

當從 Terminal Server 工作階段接收到變更事件時執行。Executes when a change event is received from a Terminal Server session.

OnShutdown()

在衍生類別中實作時,當系統正在關閉時執行。When implemented in a derived class, executes when the system is shutting down. 指定緊接在系統關閉之前應該發生的處理。Specifies what should occur immediately prior to the system shutting down.

OnStart(String[])

在衍生類別中實作時,當服務控制管理員 (SCM) 傳送啟動命令至服務或當作業系統啟動 (自動啟動服務) 時執行。When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). 指定在服務啟動時所要執行的動作。Specifies actions to take when the service starts.

OnStop()

在衍生類別中實作時,當服務控制管理員 (SCM) 傳送停止命令至服務時執行。When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). 指定在服務停止執行時所要執行的動作。Specifies actions to take when a service stops running.

RequestAdditionalTime(Int32)

要求額外時間進行暫止的運算。Requests additional time for a pending operation.

Run(ServiceBase)

使用服務控制管理員 (SCM) 登錄服務的可執行檔。Registers the executable for a service with the Service Control Manager (SCM).

Run(ServiceBase[])

使用服務控制管理員 (SCM) 登錄多個服務的可執行檔。Registers the executable for multiple services with the Service Control Manager (SCM).

ServiceMainCallback(Int32, IntPtr)

登錄命令處理常式 (Command Handler) 並啟動服務。Registers the command handler and starts the service.

Stop()

停止執行中的服務。Stops the executing service.

ToString()

傳回任何包含 Component 名稱的 StringReturns a String containing the name of the Component, if any. 不應覆寫此方法。This method should not be overridden.

(繼承來源 Component)

事件

Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)

適用於

另請參閱