【问题标题】:Assembly.LoadFrom doesn't load dll from bin\debug when running unit tests with resharper使用 resharper 运行单元测试时,Assembly.LoadFrom 不会从 bin\debug 加载 dll
【发布时间】:2016-04-26 07:58:12
【问题描述】:

我正在使用带有 Resharper Ultimate 2016.1 的 VS 2015 Update 2,我遇到了这个奇怪的问题。

我有一个名为 Test 的测试项目,它引用了两个项目,Model 和 Persistence。模型项目包含 nhibernate 实体类,Persistence 项目包含 *.hbm.xml 文件。它们是用 llblgenpro 4.2 生成的。我正在使用 nhibernate 4.0.4。

我用这个调用初始化 NHibernate:

  NHibernateSession.Init(
    new SimpleSessionStorage(), 
    new string[] { "Persistence.dll", "Model.dll" });

当我运行我的一个测试用例时,休眠初始化失败并出现以下异常:

System.IO.FileNotFoundException was unhandled by user code
  FileName=file:///C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Persistence.dll
  FusionLog==== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Persistence.dll
LOG: Appbase = file:///C:/projects/csharp/Test/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\costa\AppData\Local\Temp\s0hjyhsk.jq1\a3514fde-acb9-4c62-a0ce-a586f8202f35.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/Users/costa/AppData/Local/JetBrains/Installations/ReSharperPlatformVs14/Persistence.dll.

  HResult=-2147024894
  Message=Could not load file or assembly 'file:///C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Persistence.dll' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  StackTrace:
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
       at System.Reflection.Assembly.LoadFrom(String assemblyFile)
       at SharpArch.NHibernate.NHibernateSession.<>c__DisplayClass36_0.<CreateSessionFactoryFor>b__0(MappingConfiguration m) in C:\work\sharp-arch\Solutions\SharpArch.NHibernate\NHibernateSession.cs:line 412
       at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
  InnerException: 

如果我将 persistence.dll 复制到 C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14 文件夹,则测试用例可以正常工作。 persistence.dll 位于 C:/projects/csharp/Test/bin/Debug 文件夹中,因为在测试项目中引用了持久化项目。

这一切在 VS 2013 中使用 nhibernate 3.3.1 运行良好。此外,我使用测试项目 app.config 文件中的 assemblybinding 元素使所有 dll 版本对齐。

我的项目以 .Net 4.6 为目标,我使用 nunit 3.2.1。

我发现了这个:

Resharper runs UnitTest from different location

但是,在我的情况下,“正在测试的影子复制程序集”已关闭,对每个带有测试的程序集使用单独的 AppDomain 也已关闭。运行测试从设置为项目输出文件夹。

有什么可能导致这种情况的想法吗?

谢谢

更新:如果我这样做,它会起作用:

  string path = Assembly.GetExecutingAssembly().Location;
  string directory = Path.GetDirectoryName(path);

  NHibernateSession.Init(
    new SimpleSessionStorage(), 
    new string[] { directory + "\\Persistence.dll", directory + "\\Model.dll" });

更新 2. 我的项目使用 Sharp Architecture library。 NHibernateSession 是属于这个库的一个类。

【问题讨论】:

  • 什么是NHibernateSession?在我看来,它并不是 NHibernate 的原生部分。如果这是一些自定义类,那么如果没有它的代码就很难为您提供帮助。也许这是 llblgenpro 生成的一些类:在这种情况下,最好向他们寻求支持。
  • 也许你会更容易离开 llblgenpro 并直接使用你的映射和会话工厂。它可以让您根据您的业务需求对其进行微调。 (您可以阅读更多关于我的观点here。)并且您将有更多选项来处理您的会话工厂初始化,例如我的答案here 中的那个。
  • @Frédéric:我更新了我的帖子。 NHibernateSession 属于 s#arp-architecture 库。抱歉,我错过了。

标签: nhibernate visual-studio-2015 resharper s#arp-architecture nunit-3.0


【解决方案1】:

这可能是 NUnit 3 中的一个更改,它不再将当前目录更改为被测程序集的位置。我相信这是因为它可以在一次测试运行中运行多个程序集 - 那么哪个目录最好?

根据the NUnit 3 Breaking Changes document,您可以使用TestContext.CurrentContext.TestDirectory 定位包含被测程序集的目录。

【讨论】:

  • 我切换到 NUnit 2.6.4,它工作正常。所以一定是这个。
猜你喜欢
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多