2010-09-03 105 views
2

調用我的任何Mapper.Map方法時出現以下異常。AutoMapper/Castle - 覆蓋成員時違反了繼承安全規則

重寫成員時違反了繼承安全規則:'Castle.Core.Logging.LevelFilteredLogger.InitializeLifetimeService()'。 覆蓋方法的安全性可訪問性必須與要覆蓋的方法的安全性可訪問性相匹配 。

我使用的是在.NET 4.0(使用Castle.Core 1.2.0.6623版本)上運行的S#arp 1.6應用程序中從codeplex下載的AutoMapper的最新版本。

我相信它與我不太瞭解的新的.Net 4.0安全設置有關。

有沒有辦法解決它?

保羅

+1

你曾經嘗試都合併和未合併AutoMapper組裝?我遇到了合併後的問題 - 更改爲未合併的固定(Automapper v2.0.0.200)。 – rmac 2010-09-26 14:07:49

+0

謝謝rmacfie ...爲我解決了它。 – Daniel 2010-11-07 23:53:05

回答

1

我試圖從東西一個小小的谷歌搜索固定我的問題,我不知道這是理想還是推薦的方法,但它的工作。

我已將此添加到Automapper項目「的AssemblyInfo.cs」文件:

[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)] 

我重新編譯和使用新的DLL,一切工作正常。

請留下評論,如果這不是reccomended或如果有更好的方法。

現在雖然我會留下我自己的答案作爲正確的答案。

感謝您的幫助!

UPDATE:

我的映射是非常簡單的,對不起所有的代碼,但認爲它可以幫助你:

初始化代碼:

Mapper.Reset(); 
Mapper.Initialize(x => 
{ 
    x.AddProfile<LeadsProfile>(); 
    //x.AddProfile<AttendeeProfile>(); 
}); 

Mapper.AssertConfigurationIsValid(); 

LeadsProfile。CS

public class LeadsProfile : AutoMapper.Profile 
     { 
      public override string ProfileName 
      { 
       get { return "LeadsProfile"; } 
      } 

      protected override void Configure() 
      { 

       Mapper.CreateMap<Lead, LeadDto>(); 

       Mapper.CreateMap<Lead, LeadDetailDto>(); 

       Lead lead = null; 
       Mapper.CreateMap<int, LeadDetailDto>() 
        .BeforeMap((s, d) => lead = ServiceLocator.Current.GetInstance<ILeadRepository>().FindOne(s)) 
        .ForMember(d => d.Id, x => x.MapFrom(s => lead.Id)) 
        .ForMember(d => d.Fullname, x => x.MapFrom(s => lead.Fullname)) 
        .ForMember(d => d.TelNumber, x => x.MapFrom(s => lead.TelNumber)) 
        .ForMember(d => d.BookedAppointmentDate, x => x.MapFrom(s => lead.BookedAppointmentDate)); 

      } 

     } 

源類

public class Lead : Entity 
    { 
     public Lead() 
     { 
      Status = Common.LeadStatus.Raw; 
      CreatedDate = DateTime.Now; 
     } 

     public Lead(Branch branch, Promoter promoter, LeadSource source, string fullname, string telNumber, Address address, DateTime apptDate) : this() 
     { 
      this.Branch = branch; 
      this.Promoter = promoter; 
      this.Source = source; 
      this.Fullname = fullname; 
      this.TelNumber = telNumber; 
      this.Address = address; 
      this.BookedAppointmentDate = apptDate; 
     } 

     public virtual Branch Branch { get; set; } 
     public virtual Promoter Promoter { get; set; } 
     public virtual LeadSource Source { get; set; } 
     public virtual Common.LeadStatus Status { get; set; } 

     public virtual bool ExistingCustomer { get; set; } 
     public virtual bool IsDoso { get; set; } 

     public virtual string TitlePrefix { get; set; } 
     public virtual string Fullname { get; set; } 
     public virtual string TelNumber { get; set; } 
     public virtual string MobileNumber { get; set; } 

     public virtual DateTime BookedAppointmentDate { get; set; } 

     public virtual Address Address { get; set; } 

     public virtual Store Store { get; set; } 

     public virtual IList<LeadProduct> Products { get; set; } 
     public virtual IList<Appointment> Appointments { get; set; } 
     public virtual IList<Sale> Sales { get; set; } 

     public virtual DateTime CreatedDate { get; set; } 
    } 

目標DTO的

public class LeadDto 
    { 
     public int Id { get; set; } 
     public string Fullname { get; set; } 
     public string TelNumber { get; set; } 
     public DateTime BookedAppointmentDate { get; set; } 
    } 

public class LeadDetailDto 
    { 
     public int Id { get; set; } 
     public string Fullname { get; set; } 
     public string TelNumber { get; set; } 
     public DateTime BookedAppointmentDate { get; set; } 
    } 
+0

是否可以共享源/目標映射的示例?如果這是一個錯誤,我很樂意得到一個迴歸測試。 – 2010-09-07 12:38:25

+0

我已經用我的代碼更新了我的答案,希望它有幫助...如果可以修復這個問題很好,所以我不必重新編譯項目。 – 2010-09-07 17:02:26

+0

我在使用.NET 4的最新版本2.0.207中收到此錯誤。 – Schneider 2011-04-11 08:43:51

相關問題