Unity has no such method. When specifying an instance to return for a requested type, Unity requires a created instance to use and manages the lifetime of the object as a singleton. This means that the above functionality, where a Channel is created by a delegate to the ChannelFactory to satisfy the resolution of the IAccountService, cannot be readily implemented in Unity.

To get around this limitation, I have designed a LifetimeManager that takes as a parameter a delegate. The implementation, shown below, is simple. In the override for GetValue() the delegate is called.

Note how the NewInstanceLifetimeManager takes an underlying LifetimeManager implementation as an optional paramater. For channels I like to use a per-request context lifetime manager. I can use the NewInstanceLifetimeManager in conjunction with the ContextLifetimeManager as follows (I am using the container to resolve the ContextLifetimeManager so that the required ContextItemProvider constructor parameter is set):

Conclusion

This post has covered how to extend the functionality of the Unity IOC container to enable the getting of instances using a delegate through the use of a LifetimeManager that takes a delegate as a parameter. A useful application of this technique is for the creation of WCF Channels from channel factories.