---
title: "Load an ONNX model from a .NET embedded resource"
url: https://recursiveai.org/onemodel/cookbook/load-onnx-model-from-embedded-resource/
description: "Embed a self-contained ONNX model in a .NET assembly and load it in C# from a read-only resource stream. Keep the stream open until the model is disposed."
version: "1.3.0"
---

# Load an ONNX model from a .NET embedded resource

**OneModel** is a free, open-source neural-network library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/load-onnx-model-from-embedded-resource/load-onnx-model-from-embedded-resource.zip)

Pass the assembly’s read-only stream for `add-one.onnx` (in the zip) to `Model.Load`.

```shell
dotnet add package RecursiveAI.OneModel
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemodel/cookbook/load-onnx-model-from-embedded-resource/load-onnx-model-from-embedded-resource.zip), 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:

```xml
<ItemGroup>
  <EmbeddedResource Include="add-one.onnx"
                    LogicalName="add-one.onnx" />
</ItemGroup>
```

## The full program

```csharp
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));
```

```text
3, 5
```

Keep 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:

```csharp
// ModelResources is a type defined beside the resource.
var app = typeof(ModelResources).Assembly;
```

## Related recipes

- [Run an ONNX model in C#](https://recursiveai.org/onemodel/cookbook/run-onnx-model/)
- [Load separate ONNX weight files](https://recursiveai.org/onemodel/cookbook/load-onnx-external-weight-files/)

## 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](https://learn.microsoft.com/en-us/dotnet/core/resources/manifest-file-names) 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:

```csharp
// 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](https://recursiveai.org/onemodel/docs/onnx-import/#caller-owned-memory-and-embedded-resources), 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.
