0

我有一个针对net452net461和的测试项目netcoreapp20。一切运行良好net452netcoreapp20但是,当我运行时,net461我得到了System.TypeInitializationException

这是堆栈跟踪

Unhandled exception: System.TypeInitializationException: The type initializer for "MyApp.SomeClass" threw an exception. ---> System.ArgumentException: Path is invalid.
   in System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
   in System.IO.Path.InternalGetDirectoryName(String path)
   in SQLitePCL.NativeLibrary.MakePossibilitiesFor(String basename, Assembly assy, Int32 flags, LibSuffix suffix)
   in SQLitePCL.NativeLibrary.MyLoad(String basename, Assembly assy, Int32 flags, Action`1 log)
   in SQLitePCL.NativeLibrary.Load(String libraryName, Assembly assy, Int32 flags)
   in SQLitePCL.Batteries_V2.MakeDynamic(String name, Int32 flags)
   in SQLitePCL.Batteries_V2.DoDynamic_cdecl(String name, Int32 flags)
   in SQLitePCL.Batteries_V2.Init()

我检查了我的测试项目的项目输出,所有的 DLL 都在那里以及SQLite.Interop.dllin./x86/./x64/

顺便说一下我的主要项目,我正在使用 Microsoft.Data.Sqlite 并且我的目标是net40, net461, 和netstandard20

4

1 回答 1

0

问题似乎在于 xunit 影子复制功能如何以及 SQLitePCLRaw 如何动态加载SQLite.Interop.dll 测试运行时 xunit 创建所有 DLL 的影子副本并将它们中的每一个放置在一个单独的随机生成的临时文件夹中,即C:\Users\Administrator\AppData\Local\Temp\4c30a280-0900-4002-874b-a65591ef7c9e\4c30a280-0900-4002-874b-a65591ef7c9e\assembly\dl3\11289531\10e73523_73aed201\Some.dll

当 SqlitePCLRawSQLite.interop.dll在运行时查找时,它会在影子副本文件夹中查找,而不是在其原始位置。

解决方案是在您的测试项目中创建一个文件xunit.runner.json并将其添加到您的测试项目中,将其构建属性设置为Content并将 CopyToOutputDirectory 设置为PreserveNewest

或者,如果您是多目标并且只想禁用卷影复制,net461您可以将以下内容添加到您的测试项目文件中

<ItemGroup Condition="$(TargetFramework) == 'net461'">
    <Content Include="$(MSBuildThisFileDirectory)xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
于 2020-06-13T01:40:29.500 回答