ServiceBase 建構函式


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

public ServiceBase ();
Public Sub New ()


請勿建立ServiceBase類別的實例。Do not create an instance of the ServiceBase class. 相反地,衍生ServiceBase自,並具現化您的衍生類別。Instead, derive from ServiceBase and instantiate your derived class. 您在繼承自之ServiceBase類別的函式中所需執行的最小值是,在您的元件ServiceName上設定。The minimum you need to implement in the constructor for a class inherited from ServiceBase is to set the ServiceName on your component. 在此函數中,不需要其他處理。No other processing is specifically required in the constructor. 您應該在中處理大部分OnStart的初始化,而不是在此函式中。You should handle most initialization in OnStart rather than in the constructor. 否則,當您在服務停止後重新開機時,並不保證物件將會重新初始化。Otherwise, there is no guarantee the objects will be reinitialized when you restart a service after it has been stopped.

如果您覆寫衍生類別中的函式,請在您的程式碼中呼叫基類的函式。If you override the constructor in your derived class, call the base class constructor in your code.

ServiceBase函式AutoLogtrue將設為。The ServiceBase constructor sets AutoLog to true. 如果您不想要從服務控制管理員(SCM)自動記錄服務的呼叫,請將設定AutoLog為。 falseIf you do not want to automatically log calls to the service from the Service Control Manager (SCM), set AutoLog to false.

如果可執行檔包含單一服務,則系統會在從 SCM 選取 [啟動] 時呼叫服務的處理常式,並在呼叫 Stop 時執行此函式。If the executable contains a single service, the system calls the service's constructor when Start is selected from the SCM, and runs the destructor if Stop is called.

如果可執行檔包含多個服務,則在某個服務上呼叫 Start 會導致可執行檔中的所有服務都呼叫了這些函式,但只會啟動指定的服務。If the executable contains multiple services, calling Start on one service causes the constructors to be called for all services in the executable, but only the specified service is started. 當所有服務都已停止時,服務的析構函數會一起執行,而不是在每個服務停止時個別進行。Destructors for the services are run together when all services have been stopped, not individually when each service is stopped.


如果您覆寫基類的「函式」,您應該在衍生類別的「參數」中明確地呼叫它。If you override the base class constructor, you should explicitly call it in the constructor of your derived class.