dotnet/machinelearning · error · ArgumentException

Resource not found

Error message

Resource not found

What it means

GetEmbeddedResource reads a manifest resource embedded in the calling assembly. This library throws ArgumentException when GetManifestResourceStream returns null, meaning no embedded resource with the given name exists in that assembly. The resource name must exactly match the default namespace + folder path + file name compiled into the assembly.

Solutions

  1. Enumerate the actual names with assembly.GetManifestResourceNames() and use the exact returned string.
  2. Set the file's Build Action to 'Embedded Resource' in the .csproj (EmbeddedResource item).
  3. Build the name as '<RootNamespace>.<Folder>.<FileName>' with dots replacing slashes, matching the project's default namespace.
  4. Check the file name, case, and folder path in the resource name string.
  5. If inlining may redirect GetCallingAssembly, pass the assembly explicitly or use [MethodImpl(MethodImplOptions.NoInlining)].

Example fix

// before
var tpl = Utils.GetEmbeddedResource("llama_template.json");
// after
var asm = typeof(LlamaAgent).Assembly;
var name = asm.GetManifestResourceNames().Single(n => n.EndsWith("llama_template.json"));
var tpl = Utils.GetEmbeddedResource(name);
Defensive patterns

Strategy: validation

Validate before calling

var asm = Assembly.GetExecutingAssembly();
bool exists = asm.GetManifestResourceNames().Contains(resourceName, StringComparer.Ordinal);
if (!exists) throw new ApplicationException($"Resource '{resourceName}' missing. Available: {string.Join(", ", asm.GetManifestResourceNames())}");

Type guard

static bool ResourceExists(Assembly asm, string name) =>
    asm.GetManifestResourceNames().Any(n => n == name);

Try / catch

try { var content = Utils.GetEmbeddedResource(name); }
catch (ArgumentException ex) when (ex.ParamName == name)
{ log.Warn($"Embedded resource '{name}' not found"); }

Prevention

When it happens

Trigger: Calling GetEmbeddedResource with a resource name whose string does not exactly match an embedded resource of the calling assembly — e.g. wrong case, wrong default namespace, using a file path ('Utils/tokenizer.json') instead of the dotted resource name ('MyApp.Utils.tokenizer.json'), or the resource was not set to Embedded Resource build action.

Common situations: Developers move a JSON/model file to another folder without updating the resource name; the file's Build Action is left as 'Content' or 'None' instead of 'Embedded Resource'; project default-namespace changes rename all embedded resources; loading resources with Assembly.GetEntryAssembly assumptions that differ from GetCallingAssembly due to inlining or a different caller.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/5df87be505273040. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.ML.GenAI.Core/Utils.cs:174

        if (nRep == 1)
        {
            return x;
        }

        return x.unsqueeze(2)
                .expand(batchSize, nKVHeads, nRep, seqLen, headDim)
                .reshape(batchSize, nKVHeads * nRep, seqLen, headDim);
    }

    internal static string GetEmbeddedResource(string resourceName)
    {
        // read file content from embedded resource
        var assembly = Assembly.GetCallingAssembly();
        var resourceStream = assembly.GetManifestResourceStream(resourceName);

        if (resourceStream == null)
        {
            throw new ArgumentException("Resource not found", resourceName);
        }

        using var reader = new System.IO.StreamReader(resourceStream);
        return reader.ReadToEnd();
    }
}

View on GitHub (pinned to 7b76e69cf9)