title: How to use generic Profile with Automapper and Asp.Net Core Dependency Injection
body: <p>I would like to create the .Net Core class library that will be contains following extension method:</p>
  
  <pre><code>public static class MyServiceExtensions
      {
          public static IServiceCollection AddMyService&lt;TUserDto, TUserDtoKey, TUser, TUserKey&gt;(this IServiceCollection services)
              where TUserDto : UserDto&lt;TUserDtoKey&gt;
              where TUser : User&lt;TUserKey&gt;
          {
              services.AddAutoMapper(config =&gt;
              {
                  config.AddProfile&lt;UserMappingProfile&lt;TUserDto, TUserDtoKey, TUser, TUserKey&gt;&gt;();
              });
  
              return services;
          }
      }
  </code></pre>
  
  <p>I have following Automapper Profile:</p>
  
  <pre><code>public class UserMappingProfile&lt;TUserDto, TUserDtoKey, TUser, TUserKey&gt; : Profile 
          where TUserDto : UserDto&lt;TUserDtoKey&gt;
          where TUser : User&lt;TUserKey&gt;
      {
          public UserMappingProfile()
          {
              CreateMap&lt;TUserDto, TUser&gt;(MemberList.Destination)
                  .ForMember(x =&gt; x.Id, opts =&gt; opts.MapFrom(x =&gt; x.UserId));
  
              CreateMap&lt;TUser, TUserDto &gt; (MemberList.Source)
                  .ForMember(x =&gt; x.UserId, opts =&gt; opts.MapFrom(x =&gt; x.Id));
          }
      }
  </code></pre>
  
  <p>These entities:</p>
  
  <pre><code>public class UserDto&lt;TKey&gt;
      {
          public TKey UserId { get; set; }
  
          public string UserName { get; set; }
      }
  
  public class User&lt;TKey&gt;
      {
          public TKey Id { get; set; }
  
          public string UserName { get; set; }
      }
  
  public class MyUser : User&lt;int&gt;
      {
          public string Email { get; set; }
      }
  
  public class MyUserDto : UserDto&lt;int&gt;
      {
          public string Email { get; set; }
      }
  </code></pre>
  
  <p>If I try to use it like this:</p>
  
  <pre><code>services.AddMyService&lt;MyUserDto, int, MyUser, int&gt;();
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>{System.ArgumentException: Cannot create an instance of
    GenericMapping.Services.Mapping.UserMappingProfile<code>4[TUserDto,TUserDtoKey,TUser,TUserKey]
    because Type.ContainsGenericParameters is true.    at
    System.RuntimeType.CreateInstanceCheckThis()    at
    System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean
    wrapExceptions, Boolean skipCheckThis, Boolean fillCache)    at
    System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean
    wrapExceptions)    at
    AutoMapper.Configuration.MapperConfigurationExpression.AddProfile(Type
    profileType) in
    C:\projects\automapper\src\AutoMapper\Configuration\MapperConfigurationExpression.cs:line
    44    at
    AutoMapper.ServiceCollectionExtensions.&lt;&gt;c__DisplayClass10_0.&lt;AddAutoMapperClasses&gt;g__ConfigAction|4(IMapperConfigurationExpression
    cfg) in
    C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line
    83    at AutoMapper.MapperConfiguration.Build(Action</code>1 configure) in
    C:\projects\automapper\src\AutoMapper\MapperConfiguration.cs:line 307 
    at
    AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection
    services, Action<code>1 additionalInitAction, IEnumerable</code>1
    assembliesToScan) in
    C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line
    89    at
    GenericMapping.Services.Extensions.MyServiceExtensions.AddMyService[TUserDto,TUserDtoKey,TUser,TUserKey](IServiceCollection
    services) in
    C:\Projects\GenericMapping\GenericMapping.Services\Extensions\MyServiceExtensions.cs:line
    14    at GenericMapping.Startup.ConfigureServices(IServiceCollection
    services) in C:\Projects\GenericMapping\GenericMapping\Startup.cs:line
    33}</p>
  </blockquote>
  
  <p>How can I fix this issue?</p>