【问题标题】:SecurityException when mapping inheritance with Fluent NHibernate under Medium trust在中等信任下使用 Fluent NHibernate 映射继承时出现 SecurityException
【发布时间】:2011-04-08 19:37:02
【问题描述】:

在中等信任下运行的应用程序中映射继承时遇到问题。当我从 web.config 中删除中等信任限制时,它就像一个魅力。如果我注释掉我的子类映射,一切都会顺利。

当我的应用程序以中等信任启动时,在 Fluent 的配置构建期间,我捕获了异常,我可以从中读取 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(...) 方法没有运行权限。

我能找到的每一条信息都建议预先生成延迟代理、关闭延迟加载和禁用反射优化器。我已经完成了所有准备工作,如果我不映射子类,我的代码就可以工作。

这是我的映射:

public PageMap()
    {
        Id(x => x.PageID);
        Map(x => x.DateCreated);
        Map(x => x.DateLastAccessed);
        Map(x => x.Hits);
        Map(x => x.PrivateSuffix);
        Map(x => x.PublicSuffix);
        HasMany(x => x.Components).Not.LazyLoad();
        Not.LazyLoad();
    }
}

public class ComponentMap: ClassMap<Component>
{
    public ComponentMap()
    {
        Id(x => x.ComponentID);
        Map(x => x.Position);
        References(x => x.Page);
        DiscriminateSubClassesOnColumn("ComponentType");
        Not.LazyLoad();
    }
}

public class HeadingComponentMap : SubclassMap<HeadingComponent>
{
    public HeadingComponentMap()
    {
        Map(x => x.Text);
        Not.LazyLoad();
    }
}

以及来自 web.config 的相关部分

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<bytecode-provider type="null"/>
<reflection-optimizer use="false" />

<session-factory name="">
  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  <property name="connection.connection_string">data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|entities.mdf;User Instance=true</property>
  <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
  <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  <property name='current_session_context_class'>web</property>
</session-factory>

我必须手动完成所有操作吗?我很想在这里使用继承。

【问题讨论】:

    标签: c# nhibernate medium-trust


    【解决方案1】:

    我无法解决最初的问题,但 AlexCuse 给出的答案让我找到了解决方法。

    当我更改模型时,我使用导出映射

    .Mappings(
        m => m.FluentMappings.AddFromAssemblyOf<SessionManager>()
        .ExportTo("mappings")
    )
    

    在将映射作为资源嵌入后,我使用

    .Mappings(
        m => m.HbmMappings.AddFromAssemblyOf<SessionManager>()
    )
    

    【讨论】:

      【解决方案2】:

      嗯,看来你的基础已经覆盖了。假设您已经通过this,但包括以防万一。

      听起来您可能遇到了 fluent NHibernate 处理继承方式的问题。您正在运行什么版本(两者)?要确定问题是否特定于 fluent 或 nhibernate 的配置,您可以尝试在设置会话工厂时运行类似的操作(完全信任):

      .Mappings(m =>
      {
        m.FluentMappings
          .AddFromAssemblyOf<YourEntity>()
          .ExportTo(@"C:\your\export\path");
      
        m.AutoMappings
          .Add(/* ... */)
          .ExportTo(@"C:\your\export\path");
      })
      

      (来自http://wiki.fluentnhibernate.org/Fluent_configuration

      这将导出标准 .hbm.xml 映射文件,可用于查看应用程序是否可以在 just nhibernate

      下正常运行

      【讨论】:

        【解决方案3】:

        出现问题是因为您使用的是 SubclassMap

        Fluent NHibernate(即使在 10 月份的最新代码中)在子类映射操作期间使用 DeepClone(..) 扩展方法,该方法在内部使用 BinaryFormatter 来克隆映射表达式,但它无法在 Medium Trust 中运行,因为它试图读/写私有成员。

        我还没有修复 Fluent NHibernate(尽管我正在使用 fork 进行修复),但我最近刚刚发布了一篇关于如何在 Medium Trust 中对代码进行单元测试的帖子

        第三部分有一个可下载的程序集,其中包含可用于任何 NUnit 测试夹具的基类,它包含指向其他部分的链接:http://boxbinary.com/2011/10/how-to-run-a-unit-test-in-medium-trust-with-nunitpart-three-umbraco-framework-testing/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-23
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多