ServiceCreator(Of T) class

The generic factory the WinServicesLib dispatcher uses to instantiate the user’s service class when the SCM launches a service. T is the user’s ITbService implementation; ServiceCreator(Of T) wraps New T in a factory the dispatcher can hold by interface.

Syntax: New ServiceCreator(Of T )

T
required A user class that implements ITbService. The constraint is practical rather than syntactic — there is no Where T : ITbService clause in the source, but the factory’s CreateInstance returns New T As ITbService, which the compiler only accepts when T implements ITbService.

The class is the value typically assigned to ServiceManager.InstanceCreator:

With Services.ConfigureNew
    .Name             = "MyService"
    .InstanceCreator  = New ServiceCreator(Of MyService)    ' MyService Implements ITbService
End With

The package keeps the factory by reference; the dispatcher trampoline calls CreateInstance once per service start, immediately after the SCM has spawned the service thread. The returned instance is the object whose EntryPoint, StartupFailed, and ChangeState methods the package will route to.

See the package overview for where ServiceCreator fits in the broader lifecycle.

Methods

CreateInstance

Returns a fresh New T cast as ITbService.

Syntax: creator.CreateInstance As ITbService

User code rarely calls CreateInstance directly; the package’s dispatcher trampoline invokes it once per service start. The returned instance is owned by the dispatcher for the lifetime of the service — it is released when the service stops or the dispatcher exits.

The method has no parameters; if the user’s service class needs configuration, it should read it from the ServiceManager passed to EntryPoint rather than from constructor arguments.

Why a factory rather than a class reference

ServiceCreator(Of T) exists because the SCM dispatch model needs deferred instantiation. The configuration phase runs in Sub Main before the SCM has decided which services to start; constructing the service class eagerly there would create an unnecessary instance for services the SCM may never launch (or launch only much later). The factory defers the New T call until the service actually starts.

The same indirection lets the dispatcher pair the ITbService instance with the ServiceManager one-to-one — the trampoline can pass the service-specific ServiceManager into EntryPoint without the service class having to know about the manager at construction time.

See Also