{"record":{"id":"57c6bc10aabd216b","repo":"AvaloniaUI/Avalonia","slug":"assembly-name-needs-to-be-referenced-and-explici","errorCode":null,"errorMessage":"Assembly {name} needs to be referenced and explicitly loaded before loading resources","messagePattern":"Assembly (.+?) needs to be referenced and explicitly loaded before loading resources","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Platform/Internal/AssemblyDescriptorResolver.cs","lineNumber":39,"sourceCode":"    public IAssemblyDescriptor GetAssembly(string name)\n    {\n        if (name == null)\n            throw new ArgumentNullException(nameof(name));\n\n        if (!_assemblyNameCache.TryGetValue(name, out var rv))\n        {\n            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();\n            var match = loadedAssemblies.FirstOrDefault(a => name.Equals(a.GetName().Name, StringComparison.InvariantCultureIgnoreCase));\n            if (match != null)\n            {\n                _assemblyNameCache[name] = rv = new AssemblyDescriptor(match);\n            }\n            else\n            {\n#if NET6_0_OR_GREATER\n                if (!RuntimeFeature.IsDynamicCodeSupported)\n                {\n                    throw new InvalidOperationException(\n                        $\"Assembly {name} needs to be referenced and explicitly loaded before loading resources\");\n                }\n#endif\n                name = Uri.UnescapeDataString(name);\n                _assemblyNameCache[name] = rv = new AssemblyDescriptor(Assembly.Load(name));\n            }\n        }\n\n        return rv;\n    }\n    public void InvalidateAssemblyCache(string name)\n    {\n        _assemblyNameCache.Remove(name);\n    }\n\n    public void InvalidateAssemblyCache()\n    {\n        _assemblyNameCache.Clear();","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Platform/Internal/AssemblyDescriptorResolver.cs#L21-L57","documentation":"Thrown by AssemblyDescriptorResolver when an assembly name is requested for resource loading but is not already loaded into the AppDomain, AND the runtime does not support dynamic code (RuntimeFeature.IsDynamicCodeSupported is false, i.e. NativeAOT/trimmed scenarios). In such runtimes Assembly.Load is illegal, so the resolver cannot dynamically load the assembly and instead throws this InvalidOperationException instructing the caller to reference and explicitly load the assembly beforehand.","triggerScenarios":"On a NativeAOT or trimmed deployment (RuntimeFeature.IsDynamicCodeSupported == false), requesting resources from an assembly that is referenced but not yet loaded into the current AppDomain. The cache lookup fails and, because dynamic loading is unavailable, the resolver throws rather than attempting Assembly.Load.","commonSituations":"Avalonia apps published as NativeAOT or with trimming enabled, where a resource (e.g. an asset, style, or font) is requested from an assembly that is loaded lazily or only via reflection; multi-project setups where the resource assembly is a dependency not explicitly loaded at startup; plugins/assemblies loaded on demand.","solutions":["Explicitly load the assembly before requesting its resources: call Assembly.Load or reference a type from it (e.g. typeof(SomeType).Assembly) early at startup to force the loader.","Ensure the assembly is listed as a true reference so the NativeAOT/trim graph includes it.","Pre-load all resource assemblies during app startup initialization before any asset resolution occurs.","If possible, embed resources in the main assembly to avoid cross-assembly dynamic loading."],"exampleFix":"// before (NativeAOT): resource requested from NotYetLoaded.dll throws\n\n// after\n// Force-load the assembly at startup before resource access:\n_ = typeof(MyLibrary.MarkerType).Assembly; // roots the assembly reference\n// or explicitly: Assembly.Load(\"MyLibrary\");","handlingStrategy":"validation","validationCode":"static void EnsureAssemblyLoaded(string name)\n{\n    var loaded = AppDomain.CurrentDomain.GetAssemblies();\n    if (!loaded.Any(a => name.Equals(a.GetName().Name, StringComparison.OrdinalIgnoreCase)))\n        throw new InvalidOperationException($\"Pre-load assembly '{name}' before requesting resources.\");\n}","typeGuard":null,"tryCatchPattern":"try { return resolver.Resolve(name); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"explicitly loaded\"))\n{ /* force-load the assembly then retry */ }","preventionTips":["Pre-load all resource assemblies at app startup, especially under NativeAOT/trimming.","Reference a type from each resource assembly to root it in the trim graph.","Embed resources in the main assembly when cross-assembly loading is unreliable.","List resource assemblies as true references, not reflection-only dependencies."],"tags":["assembly-loading","nativeaot","trimming","resources","avalonia"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}