Container Creation Performance - ASP.Net Postbacks

Nov 9, 2010 at 8:00 PM

Hey guys,

Does anyone have any ideas on how to speed up CompositionContainer creation performance during ASP.Net postbacks? 

For example, I have a container that is using a directory catalog that may be used to satisfy imports on various web controls.  Since CompositionContainer and ComposablePart are both not marked as <Serializable>, I can't persist the container in Session state.  This means I would have to recreate the container on each post back to the server.  Given the lengthy time it takes to catalog a directory full of assemblies, I'm looking for a way to persist the CompositionContainer or ComposablePart data within it.

Is there anything I could try?  Any means of serializing enough data to recreate a ComposablePart or CompositionContainer would do the trick...

Thanks!

-Nick

 

 

Developer
Nov 11, 2010 at 8:08 PM

I'm not an ASP.Net developer so forgive me if this is stupid but why wouldn't you just store the Container or at least the catalog in the Application state? To more directly answer your question, there isn't any easy way to serialize that data you want without writing your own custom serializer.

Coordinator
Nov 11, 2010 at 9:22 PM

Creating a CompositionContainer should be quick.  Are you also creating the corresponding catalog on each postback?  Creating a catalog can take longer because it has to reflect over all the types in the assemblies.  On a server application you should generally try to create a catalog once when your application starts up and use the same catalog for each request.

Nov 12, 2010 at 1:39 PM
Edited Nov 12, 2010 at 1:40 PM

To answer your question dsplaisted... When I create the container, I'm also adding catalogs to it that catalog a pretty large number of types.  Creating the catalogs is what is taking the time as you suggest.  Storing the container (or Catalogs) when the server starts up is exactly what I want to do.  I'll try out ApplicationState as you suggest, because it looks like that may be exactly what I'm looking for. 

Since there is one instance of the container, I need to worry about thread safety.  What is the recommended practice for this?  I definately need to store the ComposableParts somehow in ApplicationState, but should I create the container with each post back to the server for thread safety?  Or, should I be ok with just accessing a CompositionContainer out of ApplicationState?  (http://mef.codeplex.com/Thread/View.aspx?ThreadId=71848

Thanks for the reponses!

-Nick

Nov 12, 2010 at 5:26 PM

Use the global.asax and add a static field to hold the container. Use Application_Start to initialize the container field.

That's the common practice..

Nov 15, 2010 at 8:21 PM

Thanks for the help guys!