xUnit2 runs all tests in parallel, so for this reason I want to ensure that all my automapper profiles are loaded first, in order to get a successful mapping while running the tests. This is what I have done
public static class AutoMapperRestConfiguration
{
private static readonly object Sync = new object();
private static bool isInitialized = false;
public static void Configure()
{
lock (Sync)
{
if (!isInitialized)
{
isInitialized = true;
Trace.Listeners.Add(new ConsoleTraceListener());
Trace.WriteLine("Automapper initialized");
GetConfiguration(Mapper.Configuration);
}
}
}
private static void GetConfiguration(IConfiguration configuration)
{
if (true)
{
// var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var assemblies = new List<Assembly>();
var baseProfileAssemblies = new List<Assembly>();
assemblies.Add(typeof(Profile1).Assembly);
assemblies.Add(typeof(Profile2).Assembly);
Mapper.Initialize(cfg =>
{
foreach (var assembly in assemblies)
{
var profiles = assembly.GetTypes().Where(x => typeof(Profile).IsAssignableFrom(x));
foreach (var profile in profiles)
{
cfg.AddProfile((Profile) Activator.CreateInstance(profile));
Trace.WriteLine(profile.ToString());
}
// var baseProfileSpecs = assembly.GetTypes().Where(x => typeof(BaseProfileSpecs<T>().Where(T=> )).IsAssignableFrom(x));
}
});
Mapper.Configuration.Seal();
}
}
}
}
Things work like this, but when I set the Boolean value below the GetConfiguration method call, everything starts failing. The profiles are not loaded and AutoMapper throws MappingExceptions. Isnt the lock(Sync) construct supposed to block and wait for one execution cycle to complete before the other method executes. I am not being able to wrap my head around this.