Losowo „Brak mapy konfiguracji typu lub nieobsługiwane odwzorowanie”. Błąd w Automapper

Proszę zobaczyćten post na rozwiązanie.

Ok, w końcu to zrozumiałem. Fragment mojego kodu: AppDomain.CurrentDomain.GetAssemblies () czasami nie pobiera mojego złożenia mapowania, więc gdy go brakuje, pojawia się błąd. Zastępując ten kod, zmuszając aplikację do znalezienia wszystkich złożeń, rozwiązałem mój problem.

Moja jednostka:

    /// <summary>
    /// Get/Set the name of the Country
    /// </summary>
    public string CountryName { get; set; }

    /// <summary>
    /// Get/Set the international code of the Country
    /// </summary>
    public string CountryCode { get; set; }

    /// <summary>
    /// Get/Set the coordinate of the Country
    /// </summary>
    public Coordinate CountryCoordinate { get; set; }

    /// <summary>
    /// Get/Set the cities of the country
    /// </summary>
    public virtual ICollection<City> Cities
    {
        get
        {
            if (_cities == null)
            {
                _cities = new HashSet<City>();
            }

            return _cities;
        }
        private set
        {
            _cities = new HashSet<City>(value);
        }
    }

Mój DTO:

    public Guid Id { get; set; }

    public string CountryName { get; set; }

    public string CountryCode { get; set; }

    public string Lattitude { get; set; }

    public string Longtitude { get; set; }

    public List<CityDTO> Cities { get; set; }

Moja konfiguracja

        // Country => CountryDTO
        var countryMappingExpression = Mapper.CreateMap<Country, CountryDTO>();
        countryMappingExpression.ForMember(dto => dto.Lattitude, mc => mc.MapFrom(e => e.CountryCoordinate.Lattitude));
        countryMappingExpression.ForMember(dto => dto.Longtitude, mc => mc.MapFrom(e => e.CountryCoordinate.Longtitude));

W Global.asax Application_Start mam:

        Bootstrapper.Initialise();

W Bootstrapper mam:

public static class Bootstrapper
{
    private static IUnityContainer _container;

    public static IUnityContainer Current
    {
        get
        {
            return _container;
        }
    }

    public static void Initialise()
    {
        var container = BuildUnityContainer();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

    private static IUnityContainer BuildUnityContainer()
    {
        _container = new UnityContainer();

        _container.RegisterType(typeof(BoundedContextUnitOfWork), new PerResolveLifetimeManager());

        _container.RegisterType<ICountryRepository, CountryRepository>();  

        _container.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(new ContainerControlledLifetimeManager());

        _container.RegisterType<ICountryAppService, CountryAppServices>(); 

        EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());
        var typeAdapterFactory = _container.Resolve<ITypeAdapterFactory>();
        TypeAdapterFactory.SetAdapter(typeAdapterFactory);

        return _container;
    }
}

Gdzie jest mój adapter:

public class AutomapperTypeAdapter : ITypeAdapter
{
    public TTarget Adapt<TSource, TTarget>(TSource source)
        where TSource : class
        where TTarget : class, new()
    {
        return Mapper.Map<TSource, TTarget>(source);
    }

    public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
    {
        return Mapper.Map<TTarget>(source);
    }
}

A AdapterFactory to:

    public AutomapperTypeAdapterFactory()
    {
        //Scan all assemblies to find an Auto Mapper Profile
        var profiles = AppDomain.CurrentDomain
                                .GetAssemblies()
                                .SelectMany(a => a.GetTypes())
                                .Where(t => t.BaseType == typeof(Profile));

        Mapper.Initialize(cfg =>
        {
            foreach (var item in profiles)
            {
                if (item.FullName != "AutoMapper.SelfProfiler`2")
                    cfg.AddProfile(Activator.CreateInstance(item) as Profile);
            }
        });
    }

Więc jalosowo uzyskać „Brak mapy konfiguracji typu lub nieobsługiwane mapowanie”. wskazanie błędu:

    public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
    {
        return Mapper.Map<TTarget>(source);
    }

Podczas wystąpienia tego błędulosowo trudno jest debugować i zobaczyć, co się stanie. Wiele szukałem bez odpowiedniego rozwiązania.

Błąd jest następujący:

Brak konfiguracji mapy typu lub nieobsługiwane mapowanie.

Typy mapowania: Kraj -> CountryDTO MyApp.Domain.BoundedContext.Country -> MyApp.Application.BoundedContext.CountryDTO

Ścieżka docelowa: Lista`1 [0]

Wartość źródłowa: MyApp.Domain.BoundedContext.Country

Mój projekt to projekt MVC 3 z Automapper 2.2 i Unity IoC ..

Będę wdzięczny za każdy pomysł, poradę lub rozwiązanie i podziękowania za odpowiedzi.

questionAnswers(2)

yourAnswerToTheQuestion