Embed and serve assets
On this page
Runic Assets turns a frontend build, such as Vite's dist folder, into one
validated archive inside your .NET application, and serves it from ASP.NET Core
or a Runic Desktop window. Every file keeps its media type, length, SHA-256
digest, entity tag and cache policy, so both hosts answer the same request the
same way.
Use it when the application should ship as one executable instead of an
executable and a folder, or when an ASP.NET Core application serves a single
page application. Projects created with dotnet new runic-app do not need it:
they copy the built frontend to a www folder next to the executable. Since
SDK 0.11.0 such a Views project embeds its frontend with one build property
instead, <RunicApplicationFrontendDelivery>Embedded</RunicApplicationFrontendDelivery>:
the Views build packs what it would copy to www/ with Runic Assets, and Runic
Desktop and WPF hosts, and since SDK 0.15.0 CS-WebUI, serve it without
application code. Do not also set RunicAssetsDist in such a project;
RunicDesktopHostOptions.Assets and, since SDK 0.15.0,
RunicCsWebUiHostOptions.Assets serve an archive from elsewhere.
This guide follows Runic SDK 0.11.0. The few differences in 0.6.0-preview.1 are marked where they occur.
Replace <VERSION> below with the current release from the
package catalog. The code below is quoted
from the package READMEs and checked against them.
1. Add the packages #
dotnet add package Runic.Assets --version <VERSION>
Add the adapter for the host that serves the files:
dotnet add package Runic.Assets.AspNetCore --version <VERSION>
# or, for Runic Desktop windows:
dotnet add package Runic.Assets.Desktop --version <VERSION>
Runic.Assets has no UI or web framework dependency. Each adapter brings it
along.
2. Embed the frontend build #
Build the frontend first, for example with npm run build, so its output
folder exists. Then point the .NET project at that folder:
<PropertyGroup>
<RunicAssetsDist>../Client.Web/dist</RunicAssetsDist>
</PropertyGroup>
dotnet build packs the folder into a canonical archive and embeds it as the
Runic.Assets.StaticFiles resource. The build skips packing when the files and
the project look unchanged by their timestamps, so after deleting a file from
the folder run a rebuild. The same files always produce the same bytes. Load it at
startup:
using System.Reflection;
using Runic.Assets;
AssetArchiveSource assets = AssetArchive.ReadEmbedded(
Assembly.GetExecutingAssembly());
Since SDK 0.15.0, ReadEmbedded reads the archive's manifest and ZIP
directory and inflates each asset when it is first requested, so a large
frontend no longer delays startup: for a 6 MiB frontend the load takes 0.4
instead of 15.9 ms. It no longer checks the SHA-256 digests at run time,
because the archive is part of the assembly that holds the application's code
and the build checked it when it packed it. An asset that inflates to another
length, or whose ZIP entry is damaged, fails when it is opened;
ValidateAsync() checks every digest on demand. AssetArchive.Read and
ReadAsync, for an archive file loaded at run time, still check every asset
before they return.
| Property | Use |
|---|---|
RunicAssetsDist |
Folder to pack and embed. |
RunicAssetsEntryPoint |
Entry document inside the folder. Defaults to index.html. |
RunicAssetsDistExclude |
Paths to leave out, separated by semicolons. Defaults to runic-assets.zip; setting it replaces the default, so list that file again if the folder contains it. |
RunicAssetsDefaultExcludes |
Since 0.11.0, true by default: .git directories and files whose names start with .env are never packed. Set false to pack them. |
RunicAssetsEmbeddedArchive |
Embed an archive that was packed in a separate step (see below) instead of RunicAssetsDist. |
RunicAssetsEmbeddedResourceName |
Resource name, if not Runic.Assets.StaticFiles. Pass the same name to ReadEmbedded. |
Hashed file names, such as Vite's index-Cf3tzbYH.js, are cached as immutable.
Every other file, including index.html, is revalidated with its entity tag.
A packed source map (*.map) is build warning RAS1005 since 0.11.0;
<NoWarn>$(NoWarn);RAS1005</NoWarn> silences it.
3. Serve it from ASP.NET Core #
Map the source in Program.cs:
AssetArchiveSource assets = AssetArchive.ReadEmbedded(
Assembly.GetExecutingAssembly());
app.MapRunicAssetSource(assets);
app.Run();
The endpoint serves every file at its path, the entry point at /, and the
entry point for missing paths without a file extension, such as
/settings/profile, so client-side routes survive a reload. It answers only
GET and HEAD, supports conditional and range requests, and runs after the
application's own endpoints.
Because unknown extensionless paths return the entry point, an application that
also serves an API should mount the assets under a prefix and build the
frontend with the same absolute base (Vite's base: "/ui/"):
app.MapRunicAssetSource(assets, "ui");
or serve exact paths only:
app.MapRunicAssetSource(assets, routing: new AssetRoutingOptions
{
ServeEntryPointAtRoot = false,
EnableSinglePageApplicationFallback = false,
});
Since 0.11.0, AssetRoutingOptions.ContentSecurityPolicy sends a policy with
every HTML asset (the archive carries none, because only the application knows
its origins). A minimal-API handler can return
RunicAssetResults.Asset(assets, path), and RunicAssetFileProvider exposes a
source as an IFileProvider for UseStaticFiles and other file provider
consumers; the
ASP.NET Core adapter README
shows both.
In 0.6.0-preview.1 MapRunicAssetSource serves exact manifest paths only, so
the entry point is at /index.html and there is no AssetRoutingOptions.
4. Serve it in a Runic Desktop window #
Pass the adapter's content handler as the surface content:
AssetArchiveSource assets = AssetArchive.ReadEmbedded(Assembly.GetExecutingAssembly());
await using var host = await DesktopHost.StartAsync();
await using var surface = await host.CreateSurfaceAsync(new DesktopSurfaceOptions
{
Content = new DesktopContent.Handler(assets.ToDesktopContentHandler()),
});
Paths resolve exactly as in ASP.NET Core, so one archive behaves the same on
both hosts. Pass AssetRoutingOptions to ToDesktopContentHandler to change
the rules.
In 0.6.0-preview.1 set ContentHandler = assets.ToDesktopContentHandler()
instead of Content. There, ToDesktopContentHandler extends IAssetSource
and takes DesktopAssetOptions? options; in 0.7 and 0.8 it extends
IAssetSnapshotSource, and since 0.9.0 the single source interface
IAssetSource, with AssetRoutingOptions? routing.
Serve one folder of an archive #
Since SDK 0.15.0, Subdirectory presents one folder of a source as a
frontend of its own, for an archive that holds several frontends, each built
into its own folder with its own index.html, such as a main window's and a
settings window's:
AssetArchiveSource archive = AssetArchive.ReadEmbedded(typeof(Program).Assembly, "Frontends.runic-assets");
IAssetSource main = archive.Subdirectory("main");
IAssetSource settings = archive.Subdirectory("settings");
Its paths are relative to the folder, and its entry point is the folder's
index.html or the entryPoint argument. Creating the view throws
DirectoryNotFoundException for a folder without assets and
FileNotFoundException for one without its entry point. Pass it to the
Assets option of any Runic Views host, or to ToDesktopContentHandler.
5. Pack in a separate step #
Most applications let dotnet build pack the folder. Run the packer yourself
when the archive is produced in a separate step: a CI job that builds the
frontend once for several .NET builds, an application that loads an archive
file at run time, or to inspect exactly what the build would embed. Since SDK
0.11.0 the packer is also the dotnet runic assets pack command, with the
same arguments, exit codes and JSON payload:
dotnet runic assets pack Client.Web/dist artifacts/app.runic-assets --trusted-generated-output
The packer itself ships inside the Runic.Assets package, so builds do not
need dotnet runic:
packer="$HOME/.nuget/packages/runic.assets/<VERSION>/tools/net10.0/Runic.Assets.Packer.dll"
dotnet "$packer" Client.Web/dist artifacts/app.runic-assets --trusted-generated-output
Embed the result with RunicAssetsEmbeddedArchive, or open the file at run time
with AssetArchive.Read or, since 0.11.0, AssetArchive.ReadAsync. Since SDK
0.15.0, the packer reads back every archive it writes and checks each asset
against its digest, and the build checks a RunicAssetsEmbeddedArchive with
Runic.Assets.Packer verify <archive> before it embeds it. Both
packers leave out .git and .env* unless you pass --no-default-excludes.
From .NET code, AssetArchive.WriteTrustedDirectoryAsync packs your own build
output on Linux, Windows and macOS, with AssetArchiveDirectoryOptions for the
entry point and exclusions. The
packer README
lists its options, JSON output and exit codes.
Trusted generated output #
Without --trusted-generated-output, the packer opens every file and folder
through pinned Linux handles, so a file swapped for a symbolic link while it
runs cannot redirect it outside the source folder. That mode requires Linux.
With --trusted-generated-output the packer uses ordinary file APIs and works
on Linux, Windows and macOS. It still rejects symbolic links and reparse points
it sees, but assumes that nothing changes the folder while it runs. Use it for
output of your own build or CI job that no other process can write to; the
dotnet build integration always uses it for that reason. Leave it off on Linux
when a less trusted process can write to the folder, such as a shared upload
directory.
Troubleshooting #
RAS2002:The RunicAssetsDist directory '...' does not exist.The frontend was not built beforedotnet build. Build it first, or make the build depend on it.RAS2001:Set either RunicAssetsDist or RunicAssetsEmbeddedArchive, not both.Choose one source for the embedded archive.Entry point '...' does not exist below '...' or was excluded.(RAS1002, packer exit code1) SetRunicAssetsEntryPointor--entry-pointto a file in the folder that is not excluded.Directory archive compilation requires Linux handle-pinned traversal.(RAS1004, packer exit code1) Without--trusted-generated-outputthe packer runs only on Linux. Pass the option for your own build output on Windows and macOS.RAS2006: the archive thatRunicAssetsEmbeddedArchivenames failed the packer's check. Since SDK0.15.0the build verifies the archive before embedding it; the packer'sRAS1007error above it in the build output has the reason. Pack the archive again from its source, or correct the path.- A request for
/or a client route returns404on 0.6.0-preview.1. 0.6 serves exact paths only; request/index.htmlor upgrade.
Reference #
Runic.Assets: explicit embedded resources, the development directory source, validation and inspection.Runic.Assets.AspNetCoreandRunic.Assets.Desktop: HTTP behavior and routing.- Archive format and the framework-neutral asset boundary.