title: Random "Missing type map configuration or unsupported mapping." Error in Automapper
body: <p>Please see <a href="https://stackoverflow.com/questions/16303452/automapper-random-errors/16772435#16772435">this post</a> for the solution.</p>
  
  <blockquote>
    <p>Ok, I finally figured it out. The:
    AppDomain.CurrentDomain.GetAssemblies()
    piece of my code sometimes does not get my mapping assembly so while it is  missing I get an error. Replacing this code by forcing the app to find all assemblies solved my problem.</p>
  </blockquote>
  
  <p>My Entity:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Get/Set the name of the Country
      /// &lt;/summary&gt;
      public string CountryName { get; set; }
  
      /// &lt;summary&gt;
      /// Get/Set the international code of the Country
      /// &lt;/summary&gt;
      public string CountryCode { get; set; }
  
      /// &lt;summary&gt;
      /// Get/Set the coordinate of the Country
      /// &lt;/summary&gt;
      public Coordinate CountryCoordinate { get; set; }
  
      /// &lt;summary&gt;
      /// Get/Set the cities of the country
      /// &lt;/summary&gt;
      public virtual ICollection&lt;City&gt; Cities
      {
          get
          {
              if (_cities == null)
              {
                  _cities = new HashSet&lt;City&gt;();
              }
  
              return _cities;
          }
          private set
          {
              _cities = new HashSet&lt;City&gt;(value);
          }
      }
  </code></pre>
  
  <p>My DTO:</p>
  
  <pre><code>    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&lt;CityDTO&gt; Cities { get; set; }
  </code></pre>
  
  <p>My Configuration</p>
  
  <pre><code>        // Country =&gt; CountryDTO
          var countryMappingExpression = Mapper.CreateMap&lt;Country, CountryDTO&gt;();
          countryMappingExpression.ForMember(dto =&gt; dto.Lattitude, mc =&gt; mc.MapFrom(e =&gt; e.CountryCoordinate.Lattitude));
          countryMappingExpression.ForMember(dto =&gt; dto.Longtitude, mc =&gt; mc.MapFrom(e =&gt; e.CountryCoordinate.Longtitude));
  </code></pre>
  
  <p>In Global.asax Application_Start I have:</p>
  
  <pre><code>        Bootstrapper.Initialise();
  </code></pre>
  
  <p>And in Bootstrapper I have:</p>
  
  <pre><code>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&lt;ICountryRepository, CountryRepository&gt;();  
  
          _container.RegisterType&lt;ITypeAdapterFactory, AutomapperTypeAdapterFactory&gt;(new ContainerControlledLifetimeManager());
  
          _container.RegisterType&lt;ICountryAppService, CountryAppServices&gt;(); 
  
          EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());
          var typeAdapterFactory = _container.Resolve&lt;ITypeAdapterFactory&gt;();
          TypeAdapterFactory.SetAdapter(typeAdapterFactory);
  
          return _container;
      }
  }
  </code></pre>
  
  <p>Where my adapter is:</p>
  
  <pre><code>public class AutomapperTypeAdapter : ITypeAdapter
  {
      public TTarget Adapt&lt;TSource, TTarget&gt;(TSource source)
          where TSource : class
          where TTarget : class, new()
      {
          return Mapper.Map&lt;TSource, TTarget&gt;(source);
      }
  
      public TTarget Adapt&lt;TTarget&gt;(object source) where TTarget : class, new()
      {
          return Mapper.Map&lt;TTarget&gt;(source);
      }
  }
  </code></pre>
  
  <p>And AdapterFactory is:</p>
  
  <pre><code>    public AutomapperTypeAdapterFactory()
      {
          //Scan all assemblies to find an Auto Mapper Profile
          var profiles = AppDomain.CurrentDomain
                                  .GetAssemblies()
                                  .SelectMany(a =&gt; a.GetTypes())
                                  .Where(t =&gt; t.BaseType == typeof(Profile));
  
          Mapper.Initialize(cfg =&gt;
          {
              foreach (var item in profiles)
              {
                  if (item.FullName != "AutoMapper.SelfProfiler`2")
                      cfg.AddProfile(Activator.CreateInstance(item) as Profile);
              }
          });
      }
  </code></pre>
  
  <p>So I <strong>randomly</strong> get a "Missing type map configuration or unsupported mapping." error pointing:</p>
  
  <pre><code>    public TTarget Adapt&lt;TTarget&gt;(object source) where TTarget : class, new()
      {
          return Mapper.Map&lt;TTarget&gt;(source);
      }
  </code></pre>
  
  <p>While this error occurs <strong>randomly</strong> it is hard to debug and see what happens. I have searched a lot with no proper solution.</p>
  
  <p>The error goes like:</p>
  
  <blockquote>
    <p>Missing type map configuration or unsupported mapping.</p>
    
    <p>Mapping types: Country -> CountryDTO
    MyApp.Domain.BoundedContext.Country ->
    MyApp.Application.BoundedContext.CountryDTO</p>
    
    <p>Destination path: List`1[0]</p>
    
    <p>Source value: MyApp.Domain.BoundedContext.Country</p>
  </blockquote>
  
  <p>My project is an MVC 3 project with Automapper 2.2 and Unity IoC..</p>
  
  <p>I will appreciate any idea, advice or solution and thanks for your answers.</p>