0

xUnit2 并行运行所有测试,因此我想确保首先加载我的所有自动映射器配置文件,以便在运行测试时获得成功的映射。这就是我所做的

    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。lock(Sync) 构造是否应该在另一个方法执行之前阻塞并等待一个执行周期完成。我无法解决这个问题。

4

0 回答 0