Xafari Service. Initialization and Reset
XafariCommonService.Initialize() method performs final initialization of the service, after which it is ready to work. This happens when the first request reaches the service at runtime. Until the service is initialized, it is deactivated and cannot be used. Depending on the service objectives, the developer can specify the moment of the initialization and the place where the Initialize() method will be called. For example, in the Numerators service, the initialization happens after the first request to service settings. When the service is successfully initialized, the Initialized event is generated.
Refer to the IsInitialized property of the service to find out if this server is initialized or not.
To implement the specific of the service initialization, override InitializeCore() virtual method. The example is found in the code below where the InitializeCore() method is shown inside ServicesModelService. During the service initialization, all local fields of the service fills up.
protected override void InitializeCore()
this._servicesModel = ((IModelApplication)this.CreateServicesModelLayer(ServicesRuntimeLayerName)).ServicesModel();
Protected Overrides Sub InitializeCore()
Me._servicesModel = CType(Me.CreateServicesModelLayer(ServicesRuntimeLayerName), IModelApplication).ServicesModel()
In contrary to the Setup() method that executes when the application itself is still not running, the Initialize() method is called during an active session of the app. That is why there are no limitations for the InitializeCore() method implementation.
The developer of the service should check that the Initialize() method is called after the XAF application setup completely.
To re-initialize the service, reset it.
In some cases, the service should be re-initialized. It is done through the Reset() method. Reset stands for a complete opposite of the service initialization.
All registered services are automatically reset when the XafTypesInfo.Reset() method runs.
The code below shown the implementation of a service reset in the XafariCommonService class. It runs as the user logs off.
protected virtual void LoggedOff(EventArgs e)
this.IsLoggedOn = false;
this.Active[IsLoggedOnKey] = false;
Protected Overridable Sub LoggedOff(ByVal e As EventArgs)
Me.IsLoggedOn = False
Me.Active(IsLoggedOnKey) = False
To implement additional logic for service reset, override the ResetCore() virtual method. The example below shows the code of the ResetCore() method in ServicesModelService. The reset of the service clears all local fields of the service object.