tags:

views:

53

answers:

1

I'm just starting to play with MEF and have a couple questions.

1) I wrote a WCF service that takes in some xml and passes the xml off to a parser. The parsers are composed using MEF. (metadata in the xml lets me determine which parser to use). I can add a new parser, and support new XML, by just dropping the dll in a directory. That part all works. But, WCF services can be instantiated multiple times, I want my parser catalog to be static, that is, if multiple instances of my service are spun up, and they get the same XML, I only need one instance of the parser running, they are written to be thread safe. I can't seem to configure MEF to do this. Anyone know how?

2) I can drop in a new parser into the directory and a catalog refresh will automatically discover it, that works great. But if I try to drop a modified dll into the directory, and that parser has been activated in the service, I get an error saying the file is in use. Is there a way to override this?

+2  A: 

1) It sounds like you should make your MEF container and catalogs static so they only get created once. Make sure you specify that the CompositionContainer should be thread safe by using the constructor with the isThreadSafe parameter and setting it to true.

2) You can enable shadow copying which will prevent the file from being locked when the DLL is loaded. However, you can't unload DLLs from an AppDomain in .NET, and furthermore it is not safe to recompose a CompositionContainer that can be used on multiple threads. In other words, using the isThreadSafe parameter only makes the container thread-safe for "reading"/pulling exports from the container, not modifying it via composition/recomposition.

So if you want to add a new parser it's probably best to restart the service.

Daniel Plaisted
Thanks for taking the time Daniel. Yeah, I tried number 1, didn't work. Not sure why, although thinking about it now, I wonder if I could put it in a separate class, so it's Instance based, not static, but make a reference to that class static. I'll try that.On 2, I agree. But I had hoped that dropping a DLL would be similar to dropping a web.config change, it forces a recycle automatically. But I can't even drop the dll without stopping the service.
Ken Foster