0
votes

Can we create a Custom LifeCycle using StructureMap wherein the object has to be in Singleton scope for specified preiod of time, after which the object has to be created again. In short, can we create objects every 20 or 30 mins.

1

1 Answers

0
votes

Sure, see e.g. http://www.mikeobrien.net/blog/creating-structuremap-lifecycle-for-wcf/ for an example of how to implement ILifecycle (in this case backed by WCF, but you can just as well make it thread-local, or static). You'll just have to add the logic to return a new IObjectCache instance after x minutes have passed.

Copy/paste of the code there:

public class WcfOperationLifecycle : ILifecycle
{
    public static readonly string ITEM_NAME = "STRUCTUREMAP-INSTANCES";

    public void EjectAll()
    {
        FindCache().DisposeAndClear();
    }

    public IObjectCache FindCache()
    {
        if (!OperationContext.Current.OutgoingMessageProperties.ContainsKey(ITEM_NAME))
            OperationContext.Current.OutgoingMessageProperties.Add(ITEM_NAME, new MainObjectCache());
        return (IObjectCache)OperationContext.Current.OutgoingMessageProperties[ITEM_NAME]; 
    }

    public string Scope { get { return "WcfOperationLifecycle"; } }
}