1

I'm trying to set up fluent configuration for my project; it's non-Helix and the models and maps right now are in the web project. In the GlassMapperScCustom.cs, I've added this script (that's similar to what's in the Professional Sitecore 8 Development book) to the AddMaps function:

string binPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");

foreach (string dll in Directory.GetFiles(binPath, binFiles, SearchOption.AllDirectories)) {
    try {
        Assembly loadedAssembly = Assembly.LoadFile(dll);
        Type glassmapType = typeof(IGlassMap);
        IEnumerable<Type> maps = loadedAssembly.GetTypes().Where(x => glassmapType.IsAssignableFrom(x));

        foreach (Type map in maps) {
            mapsConfigFactory.Add(() => Activator.CreateInstance(map) as IGlassMap);
        }
    } catch (FileLoadException loadEx) {
    } // The Assembly has already been loaded.
    catch (BadImageFormatException imgEx) {
    } // If a BadImageFormatException exception is thrown, the file is not an assembly.
}

Where binFiles is the matching pattern to look for. I'm being told that one of my models exists in multiple assemblies...I'm using interfaces for the models, and the model in question inherits one other interface and is inherited by one, so I don't know if that has an effect.

When I step through the code in the debugger, it only registers one assembly as having GlassMap items, and I'm not seeing any repeats, so I don't know where it could be coming from. The shared layout and main layout both call this model in the @inherits statement, but even if I comment one of those out, the error still occurs.

  • Rather than just looping through all of the files in /bin and using try/catch to ensure it's actually an assembly, why not just get all the assemblies from the current AppDomain? Like: AppDomain.CurrentDomain.GetAssemblies().<add filtering here> I'm not entirely sure if this would make a difference, but it's a thought. Maybe because you're looping through all the files in the directory there's something getting loaded twice somehow? – LonghornTaco Mar 1 '18 at 14:33
  • Not sure why, but changing it up seemed to do the trick. The function I was trying was from your book, maybe because mine's not in Helix form? I'll post what worked as the answer. – Ken McAndrew Mar 2 '18 at 2:03
2

Doing the AddMaps function like this ended up working for me (replace "DevBox" with the name of your assembly set to check):

public static void AddMaps(IConfigFactory<IGlassMap> mapsConfigFactory) {
    Type glassmapType = typeof(IGlassMap);
    IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.Contains("DevBox"));

    foreach (Assembly assembly in assemblies) {
        IEnumerable<Type> maps = assembly.GetTypes().Where(x => glassmapType.IsAssignableFrom(x));

        foreach (Type map in maps) {
            mapsConfigFactory.Add(() => Activator.CreateInstance(map) as IGlassMap);
        }
    }
}
  • Glad it helped! – LonghornTaco Mar 2 '18 at 4:14
  • I had the same issue and the proposed solution resolved it. – Tom Paterno Mar 27 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.