apache / lucenenet

Apache Lucene.NET
https://lucenenet.apache.org/
Apache License 2.0
2.17k stars 624 forks source link

The type initializer for "Lucene.Net.Diagnostics.Debugging" threw an exception #889

Open Plexois37 opened 6 months ago

Plexois37 commented 6 months ago

Is there an existing issue for this?

Describe the bug

I get the following Exception:

System.TypeInitializationException HResult=0x80131534 Message=The type initializer for "Lucene.Net.Diagnostics.Debugging" threw an exception. Source=Lucene.Net StackTrace: at Lucene.Net.Store.BaseDirectory.SetLockFactory(LockFactory lockFactory) at Lucene.Net.Store.FSDirectory.SetLockFactory(LockFactory lockFactory) at Lucene.Net.Store.FSDirectory..ctor(DirectoryInfo path, LockFactory lockFactory) at Lucene.Net.Store.MMapDirectory..ctor(DirectoryInfo path, LockFactory lockFactory, Int32 maxChunkSize) at Lucene.Net.Store.MMapDirectory..ctor(DirectoryInfo path, LockFactory lockFactory) at Lucene.Net.Store.MMapDirectory..ctor(DirectoryInfo path) ...

Inner Exception 1: FileLoadException: Could not load file or assembly "Microsoft.Extensions.Configuration.Abstractions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" or one of its dependencies. The located assembly's manifest definition does not match the assembly referencen. (Exception from HRESULT: 0x80131040)

I'm using beta16 and .Net Framework 4.8.1

It happens when I try to open a directory:

_IndexPath = FSDirectory.Open(new DirectoryInfo(Path.GetFullPath(Path.Combine(indexRootPath, subDir)))); or _IndexPath = new MMapDirectory(new DirectoryInfo(Path.GetFullPath(Path.Combine(indexRootPath, subDir))));

I tried Version 1.1.2 of Microsoft.Extensions.Configuration.Abstractions and Microsoft.Extensions.Configuration but that didn't help.

I use several projects in my solution. I searched for "Microsoft.Extensions.Configuration.Abstractions.dll" and these are all instances that were found: [0] {[D:\TestProgram\Index\IndexSearch\bin\Debug\Microsoft.Extensions.Configuration.Abstractions.dll, {Microsoft.Extensions.Configuration.Abstractions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60}]} System.Collections.Generic.KeyValuePair<string, System.Reflection.AssemblyName> [1] {[D:\TestProgram\packages\Microsoft.Extensions.Configuration.Abstractions.1.1.2\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll, {Microsoft.Extensions.Configuration.Abstractions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60}]} System.Collections.Generic.KeyValuePair<string, System.Reflection.AssemblyName>

Expected Behavior

_IndexPath = FSDirectory.Open(new DirectoryInfo(Path.GetFullPath(Path.Combine(indexRootPath, subDir)))); should open the directory instead of producing an exception

Steps To Reproduce

use beta16 and .Net Framework 4.8.1 (also happens with lower versions)

try to open a directory: private FSDirectory _IndexPath;

_IndexPath = FSDirectory.Open(new DirectoryInfo(Path.GetFullPath(Path.Combine(indexRootPath, subDir)))); or _IndexPath = new MMapDirectory(new DirectoryInfo(Path.GetFullPath(Path.Combine(indexRootPath, subDir))));

Exceptions (if any)

System.TypeInitializationException HResult=0x80131534 Message=The type initializer for "Lucene.Net.Diagnostics.Debugging" threw an exception. Source=Lucene.Net StackTrace: at Lucene.Net.Store.BaseDirectory.SetLockFactory(LockFactory lockFactory) at Lucene.Net.Store.FSDirectory.SetLockFactory(LockFactory lockFactory) at Lucene.Net.Store.FSDirectory..ctor(DirectoryInfo path, LockFactory lockFactory) at Lucene.Net.Store.MMapDirectory..ctor(DirectoryInfo path, LockFactory lockFactory, Int32 maxChunkSize) at Lucene.Net.Store.MMapDirectory..ctor(DirectoryInfo path, LockFactory lockFactory) at Lucene.Net.Store.MMapDirectory..ctor(DirectoryInfo path) ...

Inner Exception 1: FileLoadException: Could not load file or assembly "Microsoft.Extensions.Configuration.Abstractions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" or one of its dependencies. The located assembly's manifest definition does not match the assembly referencen. (Exception from HRESULT: 0x80131040)

Lucene.NET Version

Lucene.Net 4.8.0-beta00016

.NET Version

.Net Framework 4.8.1 (also happens with lower versions)

Operating System

Windows 10

Anything else?

No response

NightOwl888 commented 4 months ago

This issue happens when you have a version conflict between the version of [Microsoft.Extensions.Configuration.Abstractions]() that we target and other libraries in your project. Unfortunately, the version resolution on these older packages doesn't work well and manual fixup is often required when you pull in another dependency that depends on Microsoft.Extensions.Configuration.Abstractions or any of the other libraries in Microsoft.Extensions.Configuration.

The solution is to go through your project dependencies to determine the lowest version of Microsoft.Extensions.Configuration.Abstractions that can be targeted to resolve the conflict. You may use a higher version than this and it will work (in the master branch we are currently targeting 3.1.6, which seems to solve this).

You can then either add a direct reference to Microsoft.Extensions.Configuration.Abstractions and all of the other Microsoft.Extensions.Configuration packages to your project or use assembly binding redirects to do the same.