title: Custom Type Converter (AutoMapper) and .NET Core
body: <p>I have used the following guide to setup AutoMapper with my .NET Core Web App (DI): <a href="https://dotnetcoretutorials.com/2017/09/23/using-automapper-asp-net-core/" rel="nofollow noreferrer">https://dotnetcoretutorials.com/2017/09/23/using-automapper-asp-net-core/</a></p>
  
  <p>It all works fine and dandy - until I want to use a custom type converter. I have made the following:</p>
  
  <pre><code>public class TimeSpanConverter : ITypeConverter&lt;string, TimeSpan&gt;
  {
      public TimeSpan Convert(string source, TimeSpan destination, ResolutionContext context)
      {
          return TimeSpan.Parse(source);
      }
  } 
  </code></pre>
  
  <p>And then using the new converter with:</p>
  
  <pre><code>CreateMap&lt;string, TimeSpan&gt;().ConvertUsing(new TimeSpanConverter()); 
  </code></pre>
  
  <p>Problem is that it does not get called! I have tried moving it outside the project to a simple console application with no DI and it works fine there, so it must be something with how it is setup. Have trawled through numerous guides, but I end up at a blank.</p>
  
  <p>My mapping profile is this:</p>
  
  <pre><code>public class MappingProfile : Profile
  {
      public MappingProfile()
      {
          // Add as many of these lines as you need to map your objects
          CreateMap&lt;string, TimeSpan&gt;().ConvertUsing(new TimeSpanConverter());
          CreateMap&lt;Channel, ChannelDto&gt;();
          CreateMap&lt;Location, LocationDto&gt;();
          CreateMap&lt;LocationDto, Location&gt;();
          CreateMap&lt;OperatingSchedule, OperatingScheduleDto&gt;();
          CreateMap&lt;OperatingSchedule, AddOperatingScheduleRequest&gt;();
          CreateMap&lt;AddOperatingScheduleRequest, OperatingSchedule&gt;();
      }
  
      public class TimeSpanConverter : ITypeConverter&lt;string, TimeSpan&gt;
      {
          public TimeSpan Convert(string source, TimeSpan destination, ResolutionContext context)
          {
              return TimeSpan.Parse(source);
          }
      }
  } 
  </code></pre>
  
  <p>Under ConfigureServices i have:</p>
  
  <pre><code>services.AddSingleton&lt;HttpClient&gt;();
  services.AddTransient&lt;ITypeConverter&lt;string, TimeSpan&gt;, TimeSpanConverter&gt;();
  services.AddTransient&lt;IConfigurationRepository, ConfigurationRepositorySql&gt;();
  services.AddTransient&lt;IDataStoreRepository, DataStoreRepositoryInflux&gt;();
  services.AddTransient&lt;ITokenService, TokenService&gt;();
  services.AddMvc();
  services.AddAutoMapper(); 
  </code></pre>