The IBeforeInitializationPlugin plugin type allows you to execute code after plugins are instantiated but before plugins initialize.
What would I use IBeforeInitializationPlugin for?
This plugin type 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 and you must bind your services.
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 a plugin's Initialize() method and later. These would not be available for use when IRecurringEvolutionJobPlugin s register their default schedules or context, IConfigurablePlugin s define configuration, or ITranslatablePlugin s register their translations. Platform In-Process Apis are also not available during the before initialization process.

Creating an IBeforeInitializationPlugin
An IBeforeInitializationPlugin plugin in addition to the normal IPlugin members only have a single method, BeforeInitialization receiving a BeforeInitializationArguments object. This plugin is slightly different than most others in the sense that it is written like it's an event handler, though it is not a true event. Instead the BeforeInitialization() is a normal method invoked by the platform before plugin initialization.
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.
Service Binding Example
First and have it implement IBeforeInitializationPlugin. In our example, we'll use the ServiceLocation API which manages service bindings. In the BeforeInitialization() method, we'll create the service locator and add a service binding if it isn't created yet so it will be available later when Initialize() is called.
private IServiceLocator _serviceLocator;
#region BeforeInitializationPlugin Implementation
public void BeforeInitialize(BeforeInitializationArguments args)
{
// initialize service locator, if neccessary, before any plugin code executes.
if (_serviceLocator == null)
{
_serviceLocator = ServiceLocation.CreateServiceLocator();
_serviceLocator.Bind<ISampleService, SomeSampleService>(new SomeSampleService());
}
}
#endregion
Then, we can use the service binding in the Initialize() method:
public void Initialize()
{
// My service will be available
var service = _serviceLocator.Get<ISampleService>();
Apis.Get<IEventLog>().Write(service.SayHello(), new EventLogEntryWriteOptions());
}
Considerations
If you are writing a simple plugin that needs to utilize before initialization functionality, you can simply extend your existing plugin with IBeforeInitializationPlugin. If you have multiple plugins that all depend on before initialization logic it is recommended to use a plugin group with either an implementation of IBeforeInitializationPlugin included in the group, or the plugin group could implement IBeforeInitializationPlugin itself.
Source
This is the full source of the example used in this article.
using Telligent.Evolution.Extensibility;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.Version1;
namespace Samples
{
public class BeforeInitializationPlugin : IBeforeInitializationPlugin
{
private IServiceLocator _serviceLocator;
#region IPlugin Members
public string Name
{
get { return "BeforeInitializationPlugin Sample"; }
}
public string Description
{
get { return "Executes before plugin initialize"; }
}
public void Initialize()
{
// My service will be available
var service = _serviceLocator.Get<ISampleService>();
Apis.Get<IEventLog>().Write(service.SayHello(), new EventLogEntryWriteOptions());
}
#endregion
#region BeforeInitializationPlugin Implementation
public void BeforeInitialize(BeforeInitializationArguments args)
{
// initialize service locator, if neccessary, before any plugin code executes.
if (_serviceLocator == null)
{
_serviceLocator = ServiceLocation.CreateServiceLocator();
_serviceLocator.Bind<ISampleService, SomeSampleService>(new SomeSampleService());
}
}
#endregion
}
#region Service For Sample Purposes
public class SomeSampleService : ISampleService
{
public string SayHello()
{
return "Hello";
}
}
public interface ISampleService
{
string SayHello();
}
#endregion
}
Once and after enabling the "BeforeInitializationPlugin Sample" plugin in Administration > Extensions, you should see "Hello" in the event log in Administration > Monitoring > Events. It will re-appear if you recycle the application.