Automapper - 达到 Apppool 空闲超时 - 缺少映射

Automapper - Apppool Idle Timeout reached - Missing mapping

目前我坚持使用应用程序池超时和自动映射器。

在我的 Global.asax 中,我在 Application_start 函数中编写了以下代码。

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);
});

起初,如果我重建我的 mvc 项目并访问我的页面,它一切正常。 我的许多程序集的所有映射都按预期定义。

现在的问题: 如果我正在等待应用程序池超时(例如定义的 5 分钟)并在 5 分钟后访问我的网站,如果自动映射器试图从除主程序集之外的程序集中映射某些模型,我会得到一些 "AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping."。

解决方案结构:

来自 Web 的 AutoMapperProfiles 中定义的所有映射,引用 Web 项目和服务项目中的模型仍然在自动映射器中定义。

服务项目的 AutoMapperProfiles 中定义的所有映射(引用 ServiceModel 和实体)在自动映射器中都丢失了。

如果我打电话给 "Mapper.Configuration.GetAllTypeMaps()",我会得到以下结果 超时前:{AutoMapper.TypeMap[30]} 超时后:{AutoMapper.TypeMap[13]}

因此,在应用程序池开始休眠后,自动映射器会丢失其映射。

自动映射器配置文件示例:

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>();
        }
    }
}

有谁知道我在自动映射器实现方面做错了什么?

此致

我明白了,发现了自己的错误。 在 AutoMapper.Initialize 我写了 "AppDomain.CurrentDomain.GetAssemblies()" 这导致了 appool 回收后的问题。相反,如果它们当前未加载,我也必须使用 "BuildManager.GetReferencedAssemblies()" 来获取所有程序集。

参考link解决我的问题: