简体   繁体   中英

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

I have a test project targeting net452 , net461 , and netcoreapp20 . Everything runs fine under net452 and netcoreapp20 , however, when I run net461 I get a 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/

By the way my main project, I am using Microsoft.Data.Sqlite and am targeting net40 , net461 , and netstandard20

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM