【问题标题】:NHibernate unable to load assemblies (NHibernate.ByteCode.Castle and BusinessLogic) from GACNHibernate 无法从 GAC 加载程序集(NHibernate.ByteCode.Castle 和 BusinessLogic)
【发布时间】:2011-04-05 15:47:21
【问题描述】:

我在整个互联网上都在寻找我的这个特殊问题。我找到了一些建议的解决方案,但它们不起作用。

设置如下:

我正在使用 ActiveRecord/NHibernate。我创建了一个包装 ActiveRecord 的程序集,称为 BusinessLogic。我的想法是我所有的项目都应该使用 BusinessLogic 而不是直接引用 ActiveRecord/NHibernate。

我在 GAC(.NET 的全局程序集缓存)中安装了以下文件:

  • Antlr3.Runtime.dll BusinessLogic.dll
  • BusinessLogic.dll
  • Castle.ActiveRecord.dll
  • Castle.Components.Validator.dll
  • Castle.Core.dll
  • Castle.DynamicProxy2.dll
  • Iesi.Collections.dll log4net.dll
  • NHibernate.ByteCode.Castle.dll
  • NHibernate.dll

问题如下:当我执行从数据库读取产品信息的 ASP.NET 应用程序时,我收到以下错误消息:

Unable to load type 'NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle' during configuration of proxy factory class.
Possible causes are:
- The NHibernate.Bytecode provider assembly was not deployed.
- The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed.

如果我将 NHibernate.ByteCode.Castle.dll 的本地副本放入 ASP.NET 应用程序的 bin 文件夹中,错误就消失了。但是出现了一个新的错误。它说 NHibernate 在 BusinessLogic.dll 中找不到类(例如 Product 等)。

如果我将 BusinessLogic.dll 的本地副本放入 ASP.NET 应用程序的 bin 文件夹中,即使此错误已修复并且应用程序运行良好。

但这并不是我们所希望的,因为我不想在更新 BusinessLogic 时使用 BusinessLogic 重新编译每个应用程序。

我发现这篇文章承诺会有所帮助:http://markmail.org/message/o22r2x5fng7i6jn5#query:activerecord%20gac+page:1+mid:4rlqoicqyxjh3ypb+state:results

很遗憾,这并不能解决问题。

我将以下内容放入 machine.config 文件中:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <qualifyAssembly partialName="NHibernate.ByteCode.Castle" fullName="NHibernate.ByteCode.Castle,version=2.1.2.4000,publicKeyToken=aa95f207798dfdb4,culture=neutral" />
  </assemblyBinding>
</runtime>

错误还是一样。 NHibernate 找不到类 NHibernate.ByteCode.Castle.ProxyFactoryFactory

非常感谢任何帮助。

【问题讨论】:

  • 为什么一开始就将它们放在 GAC 中...
  • 因为它几乎被我们所有的应用程序使用。这似乎是一个很好的解决方案。经验教训:只有在 GAC 完成后才将其放入 GAC。在 GAC 中跟踪不同版本简直是天方夜谭。

标签: c# asp.net nhibernate business-logic


【解决方案1】:

我实际上在这里找到了解决方案:.NET assemblyBinding configuration ignored in machine.config

问题是我试图从 machine.config 文件中重定向到 GAC 中的程序集。看起来这被忽略了。 web.config 文件中的以下两个条目起到了作用:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <qualifyAssembly partialName="NHibernate.ByteCode.Castle" fullName="NHibernate.ByteCode.Castle,version=2.1.2.4000,publicKeyToken=aa95f207798dfdb4,culture=neutral" />
    <qualifyAssembly partialName="BusinessLogic" fullName="BusinessLogic,version=2.0.0.0,publicKeyToken=e1ee7b158bf26e98,culture=neutral"/>
  </assemblyBinding>
</runtime>

我必须使用我的业务逻辑将这些条目添加到每个应用程序的配置文件中。但现在它适用于 GAC 的程序集。

【讨论】:

    【解决方案2】:

    Nhibernate.ByteCode.* 程序集是动态加载的(它们不作为标准程序集引用),Nhibernate 在应用程序目录中查找它们。 如果您愿意,您可以检查 NHibernate 源以准确确定文件是如何找到的。

    您可以在 Visual Studio 的构建后事件中添加类似这样的内容:

    xcopy $(SolutionDir)SharedLibs\Bytecode.dll $(ProjectDir)$(OutDir) /Y /C
    

    【讨论】:

    • 你的意思是像 Assembly.Load(...) 吗?如果是这样,应该可以使用qualifyAssembly 属性:msdn.microsoft.com/en-us/library/cd71chf0.aspx MSDN 文章提到:partialName 必须与您调用中指定的名称匹配。例如,您不能在配置文件中将“math”指定为 partialName 属性并在代码中调用 Assembly.Load("math, Version=3.3.3.3")。可能是 NHibernate.ByteCode.Castle 程序集是使用版本、公钥、文化代码或这些的特定组合加载的。但是哪一个?
    • 好吧,加载中使用的名称可能是 XML 配置文件中定义的名称。编辑:是的,请在下面查看我自己的答案。
    猜你喜欢
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多