服务器会话到期时,自动映射器不起作用

时间:2018-08-29 03:21:19

标签: c# asp.net-mvc automapper

我有一个如下的类配置自动映射器:

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            // Map Entity to Model
            CreateMap<Tb_Account, PawnAccountModels>();
            CreateMap<PawnAccountModels, Tb_Account>();
        }
    }

使用autofac配置自动映射器

public class MapperModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            builder.RegisterAssemblyTypes(assemblies)
                .Where(s => typeof(Profile).IsAssignableFrom(s) && !s.IsAbstract && s.IsPublic)
                .As<Profile>();

            builder.Register(s => new MapperConfiguration(m =>
            {
                foreach (var profile in s.Resolve<IEnumerable<Profile>>())
                {
                    m.AddProfile(profile);
                }
            }));

            builder.Register(s => s.Resolve<MapperConfiguration>().CreateMapper()).As<IMapper>().SingleInstance();
        }
    }

然后我打电话给Global.asax。

var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
builder.RegisterModelBinderProvider();

builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterSource(new ViewRegistrationSource());

builder.RegisterFilterProvider();
builder.RegisterModule(new MapperModule());

 var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

我有一个问题,当初次运行Web应用程序时,它可以正常工作。但是当服务器会话过期时,我尝试在代码_mapper.Map<Tb_Account, PawnAccountModels>(account);中进行调试,它会返回异常Automapper异常:

Missing type map configuration or unsupported mapping.

我的配置或使用自动映射器时是否存在某些错误?

0 个答案:

没有答案