简体   繁体   English

在 Dot Net 4.6.1 中运行测试项目时,SQLitePCLRaw 抛出 TypeInitializationException

[英]When running test project in Dot Net 4.6.1, SQLitePCLRaw throws TypeInitializationException

I have a test project targeting net452 , net461 , and netcoreapp20 .我有一个针对net452net461netcoreapp20的测试项目。 Everything runs fine under net452 and netcoreapp20 , however, when I run net461 I get a System.TypeInitializationExceptionnet452netcoreapp20下一切运行良好,但是,当我运行net461时,我得到一个System.TypeInitializationException

here is the stack trace这是堆栈跟踪

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()

I check the project output for my test project and all the DLL's are there as well as SQLite.Interop.dll in ./x86/ and ./x64/我为我的测试项目检查项目 output 并且所有 DLL 都在那里以及SQLite.Interop.dll./x86/./x64/

By the way my main project, I am using Microsoft.Data.Sqlite and am targeting net40 , net461 , and netstandard20顺便说一句,我的主要项目是 Microsoft.Data.Sqlite 并针对net40net461netstandard20

The issue appears to be with how xunit shadow copy feature and how SQLitePCLRaw dynamically loads the SQLite.Interop.dll When the test run xunit creates a shaddow copy of all the DLL's and places each of them in a separate randomly generated temporary folder ie C:\Users\Administrator\AppData\Local\Temp\4c30a280-0900-4002-874b-a65591ef7c9e\4c30a280-0900-4002-874b-a65591ef7c9e\assembly\dl3\11289531\10e73523_73aed201\Some.dll The issue appears to be with how xunit shadow copy feature and how SQLitePCLRaw dynamically loads the SQLite.Interop.dll When the test run xunit creates a shaddow copy of all the DLL's and places each of them in a separate randomly generated temporary folder ie C:\Users\Administrator\AppData\Local\Temp\4c30a280-0900-4002-874b-a65591ef7c9e\4c30a280-0900-4002-874b-a65591ef7c9e\assembly\dl3\11289531\10e73523_73aed201\Some.dll

When SqlitePCLRaw goes to find SQLite.interop.dll at runtime it looks inside the shadow copy folder instead of its original location.当 SqlitePCLRaw 在运行时查找SQLite.interop.dll时,它会查看卷影副本文件夹而不是其原始位置。

The solution to this is to create a file xunit.runner.json in your test project and add it to your test project setting its build property to Content and setting CopyToOutputDirectory to PreserveNewest解决方案是在您的测试项目中创建一个文件xunit.runner.json并将其添加到您的测试项目中,将其构建属性设置为Content并将 CopyToOutputDirectory 设置为PreserveNewest

Alternatively, if you are multi-targeting and only want to disable shadow copy for net461 you can add the following to your test project file或者,如果您是多目标并且只想禁用net461的卷影复制,您可以将以下内容添加到您的测试项目文件中

<ItemGroup Condition="$(TargetFramework) == 'net461'">
    <Content Include="$(MSBuildThisFileDirectory)xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使SignalR与在点网框架4.6.1中运行的Angular 5配合使用 - Getting SignalR working with Angular 5 running in dot net framework 4.6.1 从.Net 4.6.1单元测试引用.Net标准项目时缺少方法异常 - Missing Method Exception When Referencing .Net Standard Project From .Net 4.6.1 Unit Test 在Docker中运行项目时获取&#39;System.TypeInitializationException&#39; - Getting 'System.TypeInitializationException' when running project in Docker 发布到IIS时无法从DotNetCore调用.NET 4.6.1项目 - Unable to call .NET 4.6.1 project from DotNetCore when published to IIS C# package for .NET 当项目使用.NETFramework 4.6.1 - C# package for .NET when the project uses .NETFramework 4.6.1 .Net 4.6.1 项目中对 exe 的引用 - Reference to exe in a .Net 4.6.1 project 如何在基于 .NET Framework 4.6.1 构建的单元测试项目中添加 .NET Core 2.1 项目引用 - How to add .NET Core 2.1 project reference in Unit Test project built on .NET Framework 4.6.1 在 Xamarin 项目中面向 .NET Framework 4.6.1 - Targeting .NET Framework 4.6.1 in Xamarin project Nunit 测试项目无法加载 System.Windows.Forms 程序集来测试 windows 表单基于应用程序定位.Net 4.6.1 - Nunit test project cannot load System.Windows.Forms assembly to test a windows form based application targeting .Net 4.6.1 尝试配置log4net时出现TypeInitializationException - TypeInitializationException when trying to config log4net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM