Search code examples
timeoutmappingautomapper

Automapper - Apppool Idle Timeout reached - Missing mapping


currently i stuck with the apppool timeout and the automapper.

In my Global.asax i wrote in the Application_start function following code.

Mapper.Initialize(cfg =>
{
    cfg.CreateMissingTypeMaps = false;

    cfg.CreateMap<string, MvcHtmlString>()
    .ConvertUsing<MvcHtmlStringConverter>();

    // Get all my project assemblies
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(
      x => x.GetName().Name.StartsWith("MyMvcApplication.")).ToArray();

    // Add all assemblies to automapper to search for defined profiles.
    cfg.AddProfiles(assemblies);
});

At first if i'm rebuild my mvc project and access my page it works all fine. All my mappings from many assemblies are defined as expected.

Now the problem: If i'm waiting for the apppool timeout (ex. defined 5 min) and access my website after the 5min i get some "AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping." if the automapper is trying to map some models from assemblies except the main assembly.

Solution Structure:

  • MyMvcApplication.DataAccess

    • Entities
  • MyMvcApplication.Services

    • ServiceModels
    • AutoMapperProfiles
  • MyMvcApplication.Web

    • Models
    • AutoMapperProfiles

All Mappings defined in the AutoMapperProfiles from the Web which referres to Models in the web project and service project are still defined in the automapper.

All mappings defined in the AutoMapperProfiles from the Service project which referres to ServiceModels and Entites are MISSING in the automapper.

If i'm calling "Mapper.Configuration.GetAllTypeMaps()" i get following results Before timeout: {AutoMapper.TypeMap[30]} After timeout: {AutoMapper.TypeMap[13]}

So the automapper loses his mappings after the apppool starting to sleep.

Example of an automapper profile:

namespace MyMvcApplication.Services.MappingProfiles
{
    using AutoMapper;
    using MyMvcApplication.DataAccess.DAL;
    using MyMvcApplication.Services.Models.Users;

    public class UserMappingProfile : Profile
    {
        public UserMappingProfile()
        {
            base.CreateMap<UserEntity, User>();
            base.CreateMap<UserEntity, BasicUser>();
            base.CreateMap<UserEntity, OverviewUser>();
            base.CreateMap<UserEntity, LoginUser>();
        }
    }
}

Does anyone know whats i'm doing wrong with the automapper implementation?

Best regards


Solution

  • I got it and found the mistake of my own. In AutoMapper.Initialize i wrote "AppDomain.CurrentDomain.GetAssemblies()" this caused the problem after the appool recyle. Instead i have to use "BuildManager.GetReferencedAssemblies()" to get all assemblies as well if they are currently not loaded.

    Reference link to solve my problem: https://stackoverflow.com/a/2479400/10236859