DirectoryCatalog already has code to catch ReflectionTypeLoadException and ignore those assemblies. Unfortunately, as I have reported merely creating the AssemblyCatalog will not yet trigger the exception so that code doesn't work The exception is actually triggered by the first call to AssemblyCatalog. Parts Instead of using the DirectoryCatalog from MEF, you will have to do it yourself: scan a directory for assemblies load each assembly and creates a AssemblyCatalog for it invoke AssemblyCatalog.
Parts to force the exception, and catch it aggregate all the good catalogs with a AggregateCatalog.
DirectoryCatalog already has code to catch ReflectionTypeLoadException and ignore those assemblies. Unfortunately, as I have reported, merely creating the AssemblyCatalog will not yet trigger the exception so that code doesn't work. The exception is actually triggered by the first call to AssemblyCatalog.Parts.
Instead of using the DirectoryCatalog from MEF, you will have to do it yourself: scan a directory for assemblies load each assembly and creates a AssemblyCatalog for it invoke AssemblyCatalog. Parts to force the exception, and catch it aggregate all the good catalogs with a AggregateCatalog.
1 Excellent! Thanks. I created a SafeDirectoryCatalog derived from AggregateCatalog and used it to load one file at a time, adding it to the Catalogs collection only if it didn't choke on accessing Parts.
Works a treat! – Phil J Pearson Nov 10 '10 at 17:40.
To save others from writing their own implementation of the SafeDirectoryCatalog, here is the one I came up with based upon Wim Coenen's suggestions: public class SafeDirectoryCatalog : ComposablePartCatalog { private readonly AggregateCatalog _catalog; public SafeDirectoryCatalog(string directory) { var files = Directory. EnumerateFiles(directory, "*. Dll", SearchOption.
AllDirectories); _catalog = new AggregateCatalog(); foreach (var file in files) { try { var asmCat = new AssemblyCatalog(file); //Force MEF to load the plugin and figure out if there are any exports // good assemblies will not throw the RTLE exception and can be added to the catalog if (asmCat.Parts.ToList(). Count > 0) _catalog.Catalogs. Add(asmCat); } catch (ReflectionTypeLoadException) { } } } public override IQueryable Parts { get { return _catalog.
Parts; } } }.
1 +1 but I would add some logging in the catch statement. You might also want to catch BadImageFormatException to ignore non-. Net DLLs.
– Wim Coenen Apr 13 at 9:08 Directory. EnumerateFiles should be Directory. GetFiles – Steve Dunn Apr 20 at 8:37 Steve, Directory.
EnumerateFiles is new in . NET 4 and has performance benefits in that entries are returned when you ask for them, instead of all at once as with . GetFiles – Dan Morphis Apr 22 at 6:27 1 Wim, that was left as an exercise to the user :-) In our production system, I did add logging :-) – Dan Morphis Apr 22 at 6:28.
I was doing this from an API I was writing and the SafeDirectoryCatalog would not log multiple exports matching a single import from different assemblies. MEF debugging is typically done via debugger and TraceListener. I already used Log4Net and I didn't want someone to need to add another entry to the config file just to support logging.
blogs.msdn.com/b/dsplaisted/archive/2010... I came up with: // I don't want people to have to add configuration information to get this logging. // I know this brittle, but don't judge... please. It makes consuing the api so much // easier.
Private static void EnsureLog4NetListener() { try { Assembly compositionAssembly = Assembly. GetAssembly(typeof (CompositionContainer)); Type compSource = compositionAssembly. GetType("System.ComponentModel.Composition.Diagnostics.
CompositionTraceSource"); PropertyInfo canWriteErrorProp = compSource. GetProperty("CanWriteError"); canWriteErrorProp.GetGetMethod(). Invoke(null, BindingFlags.
Public | BindingFlags. NonPublic | BindingFlags. Static, null, null, null); Type traceSourceTraceWriterType = compositionAssembly.
GetType( "System.ComponentModel.Composition.Diagnostics. TraceSourceTraceWriter"); TraceSource traceSource = (TraceSource)traceSourceTraceWriterType. GetField("Source", BindingFlags.
Public | BindingFlags. NonPublic | BindingFlags. Static).
GetValue(null); traceSource.Listeners. Add(new Log4NetTraceListener(logger)); } catch (Exception e) { logger.Value. Error("Cannot hook MEF compisition listener.
Composition errors may be swallowed. ", e); } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.