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.
/bin
and usingtry/catch
to ensure it's actually an assembly, why not just get all the assemblies from the currentAppDomain
? 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?