Caching a UnityContainer via AppFabric caching

Sep 21, 2011 at 9:06 PM

The container I'm using for my current project is rather expensive to build.  I was hoping to be able to stick it into an AppFabric cache slot after it has been built, however, it is not marked as Serializable/DataContract which essentially renders it incompatible.

Anyone have a workaround for this?

Sep 22, 2011 at 9:13 AM

Hi,

You will need to write a custom code to achieve that as according to this http://msdn.microsoft.com/en-us/library/ff650806.aspx

"... Alternatively, you can implement a custom scenario using the ASP.NET Cache object or even write custom code to serialize the container yourself and store it in some other location."

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 22, 2011 at 12:48 PM

Why would you want to cache the container? If you use it in a [Composition Root] there is no need to access it anywhere else. If you have an MVC-style web app you would have a container per controller so initialization should not be such an expensive task (smaller set of dependencies per controller). For WCF you can write a custom [ServiceHostFactory]. The factory is a singleton so you would only have to initialize the container once (per IIS instance). And if you (ab-)use the container as a service locator you should perhaps reconsider your architecture [ServiceLocator is an anti-pattern].

Sep 23, 2011 at 12:08 PM
Damien80 wrote:

Why would you want to cache the container? If you use it in a [Composition Root] there is no need to access it anywhere else. If you have an MVC-style web app you would have a container per controller so initialization should not be such an expensive task (smaller set of dependencies per controller). For WCF you can write a custom [ServiceHostFactory]. The factory is a singleton so you would only have to initialize the container once (per IIS instance). And if you (ab-)use the container as a service locator you should perhaps reconsider your architecture [ServiceLocator is an anti-pattern].

Thanks for the links.  Interesting.

In my situation, I'm using Unity with WorkflowFoundation and am indeed registering the Container with the WorkflowServiceHostFactory as an extension, however, there are places in the WF architecture where access to an ActivityContext is not available, nor is there any visibility to the host process so the container has to be built from scratch.