xUnit2并行运行所有测试,因此我想确保首先加载所有的automapper配置文件,以便在运行测试时获得成功的映射。这就是我所做的
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();
}
}
}
}
事情是这样的,但当我在GetConfiguration方法调用下面设置布尔值时,一切都开始失败。未加载配置文件,AutoMapper将抛出MappingExceptions。锁定(Sync)构造不应该阻塞并等待一个执行周期在另一个方法执行之前完成。我无法绕过这个。