Table of Contents

Set Initialization State on Exceptions

Note

To the Initialize() and Exit() methods a try-catch block is injected to set the InitializationState.

Before code:

public class Module : LisrtModule, IInitializable
{
    public InitializationState InitializationState { get; private set; }

    public void Initialize()
    {
        Do();
        InitializationState = InitializationState.Initialized;
    }

    public void Exit()
    {
        Do();
        InitializationState = InitializationState.NotInitialized;
    }
}

What gets compiled:

public class Module : LisrtModule, IInitializable
{
    public void Initialize()
    {
        try
        {
            Do();
            InitializationState = InitializationState.Initialized;
        }
        catch (Exception ex)
        {
            ex.AddModuleInformation(ex, this);
            InitializationState = InitializationState.Failed;
            throw;
        }
    }

    public void Exit()
    {
        try
        {
           Do();
           InitializationState = InitializationState.NotInitialized;
        }
        catch (Exception ex)
        {
            ex.AddModuleInformation(ex, this);
            InitializationState = InitializationState.NotInitialized;
            throw;
        }
    }
}

Configure the Behavior via Attribute

Use the DoNotSetInitializationStateAttribute to exclude methods from setting the InitializationState.