Skip to content

Fix unit tests native dependencies #10338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
<PropertyGroup>
<WpfTestsDir>$(MsBuildThisFileDirectory)</WpfTestsDir>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Shared\ModuleInitializer.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;

internal static class ModuleInitializer
{
/// <summary>
/// Module initializer used as a workaround for https://github.com/dotnet/runtime/issues/111825.
/// </summary>
#pragma warning disable CA2255
[ModuleInitializer]
#pragma warning restore CA2255
public static void Initialize()
{
// When a native dll fails to resolve, try loading it from the directory of the assembly loading the dll.
AssemblyLoadContext.Default.ResolvingUnmanagedDll += static (assembly, name) =>
{
return NativeLibrary.TryLoad(name, assembly, DllImportSearchPath.AssemblyDirectory, out nint handle) ? handle : 0;
};
}
}