The IBeforeInitializationPlugin is a plugin that allows you to execute code after plugins are instantiated but before plugins initialize.
[toc]
What would I use IBeforeInitializationPlugin for?
This plugin is useful anytime you have something that needs to be available to all your custom plugins before they initialize. This could be something as simple as a global variable. More commonly it is useful if you are using a Dependency Injection framework(referred to later as a DI Framework) and you must bind your services.
Required Platform DLL References:
- Telligent.Evolution.Platform.dll
Plugin Lifecycle
If you are not already you should familiarize yourself with the Plugin Lifecycle. IBeforeInitializationPlugins will execute after enabled plugins are instantiated and configured and before Apis are registered. This means any global variables (assuming static variables assigned a value or variables in the proper scope) or bound services from for your DI framework will be available in an IApiDefinition plugin if you are using one or if not in the Plugin.Initialization() method and later. These would not be available for use when IRecurringEvolutionJobs register their default schedules or context, IConfigurablePlugins define configuration, or ITranslatablePlugins register their translations. Platform In-Process Apis are also not available during the BeforeInitialization process.
[Re-Use Image]
Anatomy
An IBeforeInitializationPlugin in addition to the normal IPlugin members only have a single method, OnBeforeInitialization receiving a BeforeInitializationArguments object. This plugin is slightly different than most others in the sense that is written like its an Event handler, though it is not a true event. Instead the BeforeInitialization(...) is a normal method invoked by the platform before plugin initialization.
public interface IBeforeInitializationPlugin:IPlugin { void BeforeInitialize(BeforeInitializationArguments args); }
The BeforeInitializationArguments exposes an enumeration called PluginInitializationReason that allows you to determine why plugins are being re-initialized. Currently there are 2 reasons plugins will re-initialize:
- PluginInitializationReason.AppStart - Happens when plugins initialize because the application itself is starting or re-starting.
- PluginInitializationReason.PluginReload - Happens whenever a plugin or plugins are saved and/or enabled or disabled. This is not just your plugin, it is any plugin currently installed in your community.