AutoMapper / Castle - 覆盖成员时违反了继承安全规则

时间:2010-09-03 14:23:22

标签: .net-4.0 castle-windsor automapper s#arp-architecture

调用任何Mapper.Map方法时,我收到以下异常。

  

重写成员时违反了继承安全规则:'Castle.Core.Logging.LevelFilteredLogger.InitializeLifetimeService()'。   覆盖方法的安全性可访问性必须与   被覆盖的方法的安全可访问性。

我正在使用从.Net 4.0上运行的S#arp 1.6应用程序中的codeplex下载的最新版AutoMapper(使用Castle.Core的1.2.0.6623版本)。

我相信它与新的.Net 4.0安全设置有关,我不太明白。

有没有办法解决它?

2 个答案:

答案 0 :(得分:1)

我尝试了一些谷歌搜索来修复我的问题,我不确定这是理想的还是推荐的方法,但它确实有效。

我将此添加到Automapper项目的AssemblyInfo.cs文件中:

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

我重新编译并使用了新的DLL,一切正常。

如果没有建议,或者有更好的方法,请留下评论。

现在虽然我会将自己的答案留给正确的答案。

感谢您的帮助!

<强>更新

我的映射非常简单,对所有代码感到抱歉,但认为它可能对您有所帮助:

初始化代码:

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

答案 1 :(得分:0)

我尝试将Castle.Core升级到2.5,它已经针对.net 4.0进行了专门构建和测试。 (我应该注意到Castle.Core 2.5也适用于.net 3.5和SL 3/4)

相关问题: