stride3d/stride · error · InvalidOperationException

Couldn't find method 'PreloadLibrary' in

Error message

Couldn't find method 'PreloadLibrary' in {nativeLibraryHelperType}

What it means

LoadNativeDependencies reflects for a 'PreloadLibrary' method on Stride.Core.NativeLibraryHelper and throws an InvalidOperationException if GetMethod returns null. The preloading contract (method invoked as PreloadLibrary(libName, null)) is not present on the restored Stride.Core assembly.

Solutions

  1. Pin/restore the Stride runtime version the resolver was built against.
  2. Verify the method on the actual DLL: typeof(NativeLibraryHelper).GetMethod("PreloadLibrary", BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static).
  3. Update the resolver to call the new preload API if you upgraded Stride intentionally.
  4. Disable aggressive trimming/publish trimming for the assemblies being reflected over.

Example fix

// before
publishOptions.TrimMode = "full"; // trims PreloadLibrary away from Stride.Core
// after
publishOptions.TrimMode = "partial"; // or exclude Stride.Core from trimming
Defensive patterns

Strategy: try-catch

Validate before calling

var t = Assembly.LoadFrom(strideCorePath).GetType("Stride.Core.NativeLibraryHelper");
if (t?.GetMethod("PreloadLibrary") == null)
    throw new InvalidOperationException("Stride.Core build has no accessible PreloadLibrary — resolver/runtime mismatch or trimming removed it.");

Type guard

static bool HasPreloadLibrary(Assembly strideCore) =>
    strideCore.GetType("Stride.Core.NativeLibraryHelper")
        ?.GetMethod("PreloadLibrary") != null;

Try / catch

try
{
    resolver.SetupNuGet(packageName, packageVersion, targetFramework);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Couldn't find method 'PreloadLibrary'"))
{
    logger.Error("PreloadLibrary missing on Stride.Core.NativeLibraryHelper; check version alignment and trimming settings.");
    throw;
}

Prevention

When it happens

Trigger: nativeLibraryHelperType was found but has no 'PreloadLibrary' method with a bindable signature (renamed, internal, or parameter changes) in the restored Stride.Core version.

Common situations: Stride runtime upgraded ahead of the NuGetResolver reflection contract; custom Stride.Core forks without PreloadLibrary; strict trimming removing the method.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/384daf6f909f872a. Report an issue: GitHub.

Appendix: source

Thrown at sources/shared/Stride.NuGetResolver/NuGetAssemblyResolver.cs:289

        var nativeLibraryHelperType = strideCoreAssembly.GetType("Stride.Core.NativeLibraryHelper")
            ?? throw new InvalidOperationException($"Couldn't find type 'Stride.Core.NativeLibraryHelper' in {strideCoreAssembly}");
        var registerDependencyMethod = nativeLibraryHelperType.GetMethod("RegisterDependency")
            ?? throw new InvalidOperationException($"Couldn't find method 'RegisterDependency' in {nativeLibraryHelperType}");
        foreach (var lib in nativeLibs)
            registerDependencyMethod.Invoke(null, [lib]);
    }

    /// <summary>
    /// Loads the listed native libs in Stride.Core.NativeLibraryHelper using reflection to avoid a compile time dependency on Stride.Core
    /// </summary>
    private static void LoadNativeDependencies(Dictionary<string, string> assemblyNameToPath, List<string> nativeLibs)
    {
        var strideCoreAssembly = Assembly.LoadFrom(assemblyNameToPath["Stride.Core"])
            ?? throw new InvalidOperationException("Couldn't find assembly 'Stride.Core' in restored packages");
        var nativeLibraryHelperType = strideCoreAssembly.GetType("Stride.Core.NativeLibraryHelper")
            ?? throw new InvalidOperationException($"Couldn't find type 'Stride.Core.NativeLibraryHelper' in {strideCoreAssembly}");
        var preloadLibraryMethod = nativeLibraryHelperType.GetMethod("PreloadLibrary")
            ?? throw new InvalidOperationException($"Couldn't find method 'PreloadLibrary' in {nativeLibraryHelperType}");
        foreach (var lib in nativeLibs)
        {
            var libName = Path.GetFileNameWithoutExtension(lib);
            preloadLibraryMethod.Invoke(null, [libName, null]);
        }
    }

    public class Logger : ILogger
    {
        private readonly object logLock = new();
        private Action<LogLevel, string>? action;
        public List<(LogLevel Level, string Message)> Logs { get; } = [];

        public void SetupLogAction(Action<LogLevel, string> action)
        {
            lock (logLock)
            {
                this.action = action;

View on GitHub (pinned to 96fad776d2)