Suppose I have a MEF composition like this:
public class Composition
{
[ImportMany(AllowRecomposition = true)]
IEnumerable<ILongRunningProcess> Processes { get; set; }
public static void Main(string[] args)
{
var composition = new Composition();
using (var catalog = new DirectoryCatalog("."))
{
using (var container = new CompositionContainer(catalog)
{
container.SatisfyImportsOnce(composition);
//Fire off long running processes in response to stimuli
}
}
}
}
According to the documentation I've seen, I "have to consider thread-safety when using Recomposition."[MSDN]
Obviously for the case where I removed a type, I need to make sure that my long-running process can be garbage-collected safely. But for types that existed before the recomposition and still exist after the recomposition, will I get new instances back for the contents of Processes
whenever recomposition occurs, or is MEF able to preserve existing instances of imports when a catalog is recomposed?
Based on the above article's suggestion that for ICollection<T>
MEF will use the Clear()
and Add(T)
methods, I'm not hopeful, but I'd like to know for certain before I go write the synchronization code.
EDIT I just realized I can't use this
in a static method; I've updated the code accordingly :)