We have a RESTful and SOAPy API so having a WCF operation lifecycle in StructureMap was imperative. Creating lifecycles in StructureMap is cake. You can find some examples under the Pipeline namespace in the StructureMap source code. All you do is create a class that implements StructureMap.Pipeline.ILifecycle. Below is a lifecycle that is based on a WCF operation:

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"; } }
}

I used the existing HttpContextLifecycle class as my starting point. The important part of this class is the FindCache() method which locates the object cache. As you can see above we are stashing the object cache in the outgoing message properties. I'm not sure what the significance of the Scope property is other than debugging perhaps, but from what I can see you just return a descriptive string.

Registering this is also very simple; simply specify this as the lifecycle you'd like to use:

ObjectFactory.Configure(
    config => config.For<INumberGenerator>().
              LifecycleIs(new WcfOperationLifecycle()).
              Use<NumberGenerator>());

That's all there is to it!