ASP.NET - Automapper无法在高效的IIS上工作

时间:2014-03-20 15:19:36

标签: asp.net iis automapper

我使用我的ASP.NET MVC4应用程序中的Automapper将数据从我的域模型映射到我的视图模型。
在我的集成IIS的开发机器上,这非常有效,但是当它部署到真正的IIS时,突然我的代码只能在任意场合映射数据。
看起来映射在重新部署应用程序之后有效,有时在连接远程调试器之后。

我的代码是:

iPXE dbCon = new iPXE();
Models.Device bootDevice = dbCon.Devices.SingleOrDefault(d => d.serial == serial);
var imagePredicate = PredicateBuilder.False<Models.Image>();
imagePredicate = imagePredicate.Or(d => d.anyboot == true); // Images everyone can boot
if (bootDevice != null) // If the boot device is in the database
{
    imagePredicate = imagePredicate.Or(d => d.Devices.Any(e => e.id == bootDevice.id)); // Retrieve all images assigned to the device
}
IEnumerable<Models.Image> allowedImages = dbCon.Images.AsExpandable().Where(imagePredicate);

Mapper.CreateMap<Models.Category, Category>()
    .ForMember(x => x.name, opt => opt.MapFrom(src => src.CategoryTranslations.SingleOrDefault(d => d.locale == locale) == null || // Either no translation row found
                                                      src.CategoryTranslations.SingleOrDefault(d => d.locale == locale).name != null ? // Or specific value is null
                                                      src.name : // Then use name from main table (untranslated)
                                                      src.CategoryTranslations.SingleOrDefault(d => d.locale == locale).name)) // Otherwise use translated value
    .ForMember(x => x.images, opt => opt.MapFrom(src => allowedImages.Intersect(src.Images)));
Mapper.CreateMap<Models.Image, Image>()
    .ForMember(x => x.description, opt => opt.MapFrom(src => src.ImageTranslations.SingleOrDefault(d => d.locale == locale) == null ||
                                                             src.ImageTranslations.SingleOrDefault(d => d.locale == locale).description == null ? 
                                                             src.description : 
                                                             src.ImageTranslations.SingleOrDefault(d => d.locale == locale).description))
    .ForMember(x => x.code, opt => opt.MapFrom(src => src.ImageTranslations.SingleOrDefault(d => d.locale == locale) == null ||
                                                      src.ImageTranslations.SingleOrDefault(d => d.locale == locale).code == null ? 
                                                      src.code : 
                                                      src.ImageTranslations.SingleOrDefault(d => d.locale == locale).code));

categories = Mapper.Map<IEnumerable<Models.Category>, List<Category>>(allowedImages.Select(c => c.Category1).Distinct());

public class Category
{
    public string name { get; set; }
    public List<Image> images { get; set; }
}

public class Image
{
    public string tag { get; set; }
    public string description { get; set; }
    public string code { get; set; }
}

我所做的基本上是从数据库中获取设备,获取设备的所有已分配图像(N:M),然后为属于该设备的类别和图像创建Automapper-Maps。

确切的问题是,在生产服务器上,即使allowedImages(5张图片)中有数据bootDevice(设备数据),图像 - 类别对象的列表也会保持为空和allowedImages.Select(c => c.Category1).Distinct()(2个类别)。

我为我的机器和服务器使用相同的SQL服务器。如果您需要更多信息,请随时提出。

1 个答案:

答案 0 :(得分:1)

您正在呼叫&#39; CreateMap&#39;每次方法运行。据我了解,您只应在应用程序域中执行此操作一次。尝试将CreateMap逻辑移动到从Application_OnStart调用它的位置,看看是否能为您提供更一致的行为。

另外,该行:

.ForMember(x => x.images, opt => opt.MapFrom(src => allowedImages.Intersect(src.Images)));

可能会让你烦恼。您可能需要将其更改为调用静态方法的AfterMap,该方法在源和放大器上执行相交。目的地集合。