Repository navigation
System.InvalidOperationException : A resolver is already set for the assembly. #1855
Description
Activity
Where is this
SetDllImportResolvermethod? And why is it called twice when it's called by a static constructor?Ok, the callstack for the original exception is:
System.Private.CoreLib.dll!System.ThrowHelper.ThrowArgumentException(System.ExceptionResource resource) Line 200 C# System.Private.CoreLib.dll!System.Runtime.CompilerServices.ConditionalWeakTable<System.__Canon, System.__Canon>.Add(System.__Canon key, System.__Canon value) Line 85 C# System.Private.CoreLib.dll!System.Runtime.InteropServices.NativeLibrary.SetDllImportResolver(System.Reflection.Assembly assembly, System.Runtime.InteropServices.DllImportResolver resolver) Line 225 C# [Native to Managed Transition] [Managed to Native Transition] > LibGit2Sharp.dll!LibGit2Sharp.Core.NativeMethods.TryUseNativeLibrary() Line 127 C# LibGit2Sharp.dll!LibGit2Sharp.Core.NativeMethods.NativeMethods() Line 37 C# [Native to Managed Transition] [Managed to Native Transition] LibGit2Sharp.dll!LibGit2Sharp.Core.Proxy.git_repository_init_ext(LibGit2Sharp.Core.FilePath workdirPath, LibGit2Sharp.Core.FilePath gitdirPath, bool isBare) Line 2487 C# LibGit2Sharp.dll!LibGit2Sharp.Repository.Init(string path, bool isBare) Line 498 C# LibGit2Sharp.dll!LibGit2Sharp.Repository.Init(string path) Line 485 C# NerdBank.GitVersioning.Tests.dll!RepoTestBase.InitializeSourceControl(string repoPath, bool withInitialCommit) Line 108 C#The LibGit2Sharp part of this stack includes:
libgit2sharp/LibGit2Sharp/Core/NativeMethods.cs
Lines 123 to 125 in 3d72762
// NativeMethods.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDll); object resolveDelegate = typeof(NativeMethods).GetMethod(nameof(ResolveDll), BindingFlags.NonPublic | BindingFlags.Static).CreateDelegate(dllImportResolverType); setDllImportResolver.Invoke(null, new object[] { typeof(NativeMethods).Assembly, resolveDelegate }); This stack includes a static constructor, so the guarantee should be that it only runs once. But maybe other code calls this
NativeLibrary.SetDllImportResolveras well before the static constructor runs.Also of note, this problem only manifests on .NET Core (3.1 or 5.0).
Ah yes, my own unit tests were calling that same
NativeLibrary.SetDllImportResolvermethod. I suppose it was from before libgit2sharp did it on its own? Anyway, once I remove my code that calls it, the tests pass even with the libgit2sharp upgrade. 🥳Reacted by Edward Thomson
After updating Nerdbank.GitVersioning to libgit2sharp 0.27.0-preview-0096, a great many of my tests are failing with this exception. Is it expected that
SetConfigSearchPathsbecome callable-once-only?Originally posted by @AArnott in #1618 (comment)