DirectoryModuleCatalog Bug?

Topics: Prism v4 - WPF 4
Jul 20, 2011 at 7:25 PM

I was trying to use the DirectoryModuleCatalog, and could not get it to load my module assemblies. I looked at the code, and noticed that the issue (at least on the surface), stems from the following code in the GetNotAllreadyLoadedModuleInfos function:

private static IEnumerable<ModuleInfo> GetNotAllreadyLoadedModuleInfos(DirectoryInfo directory, Type IModuleType)
{
    ...

    var fileInfos = directory.GetFiles("*.dll")
        .Where(file => alreadyLoadedAssemblies
                           .FirstOrDefault(
                           assembly =>
                           String.Compare(Path.GetFileName(assembly.Location), file.Name,
                                          StringComparison.OrdinalIgnoreCase) == 0) == null);

    ...
}

Shouldn't the comparison for FirstOrDefault be != null, i.e. as follows:

    var fileInfos = directory.GetFiles("*.dll")
        .Where(file => alreadyLoadedAssemblies
                           .FirstOrDefault(
                           assembly =>
                           String.Compare(Path.GetFileName(assembly.Location), file.Name,
                                          StringComparison.OrdinalIgnoreCase) == 0) != null);

 

Once I change it to that, it works as expected. Am I doing something wrong, or is this a bug?

Some input would be much appreciated.

Developer
Jul 21, 2011 at 2:25 PM

Hi,

Based on my understanding of the code in the GetNotAlreadyLoadedModuleInfos method of the DirectoryModuleCatalog, the aforementioned comparison checks that the fileInfos that are added to that field are those that DO NOT exist in the alreadyLoadedAssemblies (since it's trying to obtain the NOT already loaded module infos). That's why it equals to null.

The undesired behavior you're experiencing might be happening due to the fact that you're already loading those modules from another module catalog. If that's not the case, and if this doesn't help you find the cause for your problem, it would be helpful if you could provide us with a repro sample portraying this situation (you could upload it to Skydrive, for example).

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 27, 2011 at 6:45 PM

Hi,

Thank you for your response. The bug indeed was on my end. What happened was the module DLL was inadvertently loaded before DirectoryModuleCatalog initialization, and was thus not being picked up properly by the function in question.