【问题标题】:AutoMapper / Castle - Inheritance security rules violated while overriding memberAutoMapper / Castle - 覆盖成员时违反继承安全规则
【发布时间】:2012-01-31 18:21:19
【问题描述】:

调用我的任何 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 安全设置有关。

有办法解决吗?

保罗

【问题讨论】:

  • 您是否尝试过合并和未合并的 AutoMapper 程序集?我在合并后遇到了这个问题 - 更改为未合并修复它(Automapper v2.0.0.200)。
  • 感谢 rmacfie ...为我解决了这个问题。

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


【解决方案1】:

我尝试了一些谷歌搜索来解决我的问题,我不确定这是否是理想的或推荐的方法,但它有效。

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

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

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

如果不建议这样做或有更好的方法,请离开 cmets。

虽然现在我会留下我自己的答案作为正确的答案。

感谢您的帮助!

更新:

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

初始化代码:

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

【讨论】:

  • 能否分享一个源/目标映射的示例?如果这是一个错误,我很乐意进行回归测试。
  • 我已经用我的代码更新了我的答案,希望对您有所帮助...如果可以解决这个问题那就太好了,这样我就不必重新编译项目了。
  • 将这个属性添加到源中。
【解决方案2】:

我会尝试将 Castle.Core 升级到 2.5,它是专门针对 .net 4.0 构建和测试的。 (我应该注意到 Castle.Core 2.5 也可用于 .net 3.5 和 SL 3/4)

相关问题:

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多