Pass the assembly’s read-only stream for add-one.onnx (in the zip) to Model.Load.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System.Reflection; using RecursiveAI.OneModel; var app = Assembly.GetExecutingAssembly(); using var stream = app .GetManifestResourceStream("add-one.onnx") ?? throw new FileNotFoundException(); using var model = Model.Load(stream); Console.WriteLine(string.Join(", ", model.Run([2f, 4f])));
Unzip the example, then dotnet run.
The graph adds 1 to each input, printing 3, 5. To use the snippet in your own project, add this to the .csproj; the download already includes it:
<ItemGroup> <EmbeddedResource Include="add-one.onnx" LogicalName="add-one.onnx" /> </ItemGroup>
The full program
using System.Reflection; using RecursiveAI.OneModel; var app = Assembly.GetExecutingAssembly(); using var stream = app.GetManifestResourceStream( "add-one.onnx") ?? throw new FileNotFoundException( "Missing embedded add-one.onnx"); using var model = Model.Load(stream); float[] y = model.Run([2f, 4f]); Console.WriteLine(string.Join(", ", y));
3, 5Keep the read-only stream open until you dispose the model. If the resource name doesn’t match the manifest, GetManifestResourceStream returns null; this example throws FileNotFoundException with the missing name.
Variations
For a resource embedded in a class library, get the assembly from a type defined in that project, not from the executable. If the project sets the same LogicalName as above, the rest of the code stays the same:
// ModelResources is a type defined beside the resource. var app = typeof(ModelResources).Assembly;
Related recipes
The short answer
Mark the self-contained ONNX file as EmbeddedResource, give it a LogicalName, and pass the stream returned by GetManifestResourceStream directly to Model.Load. You don’t need to extract the model to disk or copy it to a byte array first. Declare the stream before the model so C# disposes the model first.
Why does GetManifestResourceStream return null?
Check the build action, assembly, and exact name. The resource must be embedded, not just copied to the build output. In the sample, LogicalName sets the name to add-one.onnx. With no explicit name, the build can use a name derived from the root namespace and relative path. Microsoft’s manifest resource naming documentation explains when those defaults apply and how LogicalName overrides them.
The stream lookup uses the name with its exact casing. If you aren’t sure what name the build chose, list the names in the assembly you’re actually opening:
// app is the Assembly that should contain the ONNX model. Console.WriteLine(string.Join("\n", app.GetManifestResourceNames()));
If that list is empty or doesn’t contain your file, check the project that owns the EmbeddedResource item. When the ONNX lives in a referenced library’s assembly, calling GetExecutingAssembly() from the executable looks in the wrong place.
How do you load an ONNX file from a stream without copying it first?
Pass the original stream straight to Model.Load(stream). According to the ONNX import documentation, OneModel can skip copying the serialized file for an UnmanagedMemoryStream that exposes its pointer, including embedded assembly resources. It reads from the current position through the end. Ordinary streams use buffered loading instead. This saves a copy of the serialized input; the runtime can still prepare weights for inference.
The model does not close your stream. Don’t return a live model from a helper that has already disposed the resource stream with using. Keep ownership of both objects in the same scope, or arrange for their owner to dispose the model before the stream. A self-contained .onnx is the right input for this setup: embedding the graph doesn’t embed any separate weight files it references.