Skip to content

Instantly share code, notes, and snippets.

@glikoz
Created October 23, 2013 14:19
Show Gist options
  • Save glikoz/7119699 to your computer and use it in GitHub Desktop.
Save glikoz/7119699 to your computer and use it in GitHub Desktop.
/// <summary>
/// Represents a lifetime manager for request base life time management
/// </summary>
public class PerRequestLifetimeManager : LifetimeManager
{
#region Members
private LifetimeManager _WrappedLifetimeManager;
#endregion
#region Public Methods
/// <summary>
/// Gets value
/// </summary>
/// <returns></returns>
public override object GetValue()
{
EnsureLifetimeManager();
return _WrappedLifetimeManager.GetValue();
}
/// <summary>
/// Remove value
/// </summary>
public override void RemoveValue()
{
EnsureLifetimeManager();
_WrappedLifetimeManager.RemoveValue();
}
/// <summary>
/// Sets value
/// </summary>
/// <param name="newValue"></param>
public override void SetValue(object newValue)
{
EnsureLifetimeManager();
_WrappedLifetimeManager.SetValue(newValue);
}
#endregion
#region Private Methods
/// <summary>
/// Ensures that lifetime manager which is wrapped is created
/// </summary>
private void EnsureLifetimeManager()
{
if (_WrappedLifetimeManager == null)
{
if (HttpContext.Current != null)
{
_WrappedLifetimeManager = new PerWebRequestLifetimeManager();
}
else if (OperationContext.Current != null)
{
_WrappedLifetimeManager = new PerWCFRequestLifetimeManager();
}
else
{
_WrappedLifetimeManager = new PerCallContextLifetimeManager();
}
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment