Xafari Service. Initialization and Reset

Initialization

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.

Note

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.

  • c#
  • VB

protected override void InitializeCore()
{
  base.InitializeCore();
  this._servicesModel = ((IModelApplication)this.CreateServicesModelLayer(ServicesRuntimeLayerName)).ServicesModel();
}

Protected Overrides Sub InitializeCore()
  MyBase.InitializeCore()
  Me._servicesModel = CType(Me.CreateServicesModelLayer(ServicesRuntimeLayerName), IModelApplication).ServicesModel()
End Sub

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.

Important

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.

Reset

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.

It is possible to reset services automatically (for example, when the user is changed) or by demand (when service settings are changed and so on).

After being reset, the service becomes uninitialized and deactivated. The Resetted event is generated after the service was successfully reset.

Note

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.

  • c#
  • VB

protected virtual void LoggedOff(EventArgs e)
{
  this.IsLoggedOn = false;
  this.Active[IsLoggedOnKey] = false;
  this.Reset();
}

Protected Overridable Sub LoggedOff(ByVal e As EventArgs)
  Me.IsLoggedOn = False
  Me.Active(IsLoggedOnKey) = False
  Me.Reset()
End Sub

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.

  • c#
  • VB

protected override void ResetCore()
{
  this._servicesModel = null;
  base.ResetCore();
}

Protected Overrides Sub ResetCore()
  Me._servicesModel = Nothing
  MyBase.ResetCore()
End Sub