Bit.Butil 10.6.0-pre-03

This is a prerelease version of Bit.Butil.
dotnet add package Bit.Butil --version 10.6.0-pre-03
                    
NuGet\Install-Package Bit.Butil -Version 10.6.0-pre-03
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bit.Butil" Version="10.6.0-pre-03" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bit.Butil" Version="10.6.0-pre-03" />
                    
Directory.Packages.props
<PackageReference Include="Bit.Butil" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bit.Butil --version 10.6.0-pre-03
                    
#r "nuget: Bit.Butil, 10.6.0-pre-03"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bit.Butil@10.6.0-pre-03
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bit.Butil&version=10.6.0-pre-03&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bit.Butil&version=10.6.0-pre-03&prerelease
                    
Install as a Cake Tool

bit Butil

The browser platform, in C#. Butil wraps the Web APIs a Blazor app actually needs - the DOM, storage, media, sensors, crypto, workers - as injectable, strongly-typed, XML-documented services, so you can stop writing IJSRuntime.InvokeVoidAsync("someGlobal.someFunction", ...) and start writing C#.

Works on Blazor WebAssembly, Blazor Server, Blazor Hybrid and under prerendering, on .NET 8, 9 and 10.


Getting started

Install the package:

dotnet add package Bit.Butil

Add its script tag to your host page, before the Blazor script so window.BitButil exists by the time the app boots (or skip the tag entirely with lazy scripts - see Shipping only the JavaScript you use):

<script src="_content/Bit.Butil/bit-butil.js"></script>
<script src="_framework/blazor.web.js"></script>

Register the services:

using Bit.Butil;

builder.Services.AddBitButilServices();

Then inject whatever you need:

@inject Bit.Butil.Window window
@inject Bit.Butil.LocalStorage localStorage
@inject Bit.Butil.Clipboard clipboard

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender is false) return;

        await localStorage.SetItem("last-visit", DateTime.UtcNow.ToString("O"));
        await clipboard.WriteText("copied from C#");
        await window.AddEventListener(ButilEvents.KeyDown, args => { /* ... */ });
    }
}

What's in the box

Every wrapper below is an injectable service in the Bit.Butil namespace. They are registered as scoped, which matches Blazor's one-circuit-or-one-WASM-app-per-user model.

AddBitButilServices is trimming-aware: it discovers the services by reflecting over the Bit.Butil assembly for classes marked [ButilService] rather than naming them in AddScoped<T>() calls. A hard-coded call is a static reference that roots the class, so listing all of them would force every published app to carry all of them; reflecting instead lets the trimmer remove the classes your code never injects, and what it removed is simply not there to register. Injecting a Butil class from code the trimmer removed - or resolving one purely by reflection - therefore fails at runtime rather than at build time. Untrimmed apps (Blazor Server, and the prerendering host of a WebAssembly app) keep registering everything.

Window & browsing

Service What it wraps
Window The DOM window: events, dialogs, sizes, matchMedia, scrolling, printing, selection
Document The DOM document: title, cookies, fullscreen, visibility, design mode, pointer lock
History Session history: back/forward, pushState/replaceState, popstate
Navigation The Navigation API: read the history entry list, traverse to a key, and know whether you can go back
Location Read and mutate the current URL, reload, navigate
Navigator Identity, languages, share, vibrate, badges, sendBeacon, device memory
UserAgent Parsed user-agent brands, platform and mobile-ness (UA Client Hints)

Screen & diagnostics

Service What it wraps
Screen Physical screen metrics, colour depth, availability
ScreenOrientation Read, lock and observe the screen orientation
VisualViewport The visual viewport: scale, offsets, resize and scroll events
Performance High-resolution timing, marks, measures, PerformanceObserver
Reporting ReportingObserver: deprecation, intervention and crash reports
Console The full browser console API: log, table, group, time, count, profile

DOM & interaction

Service What it wraps
ElementReference extensions Attributes, scrolling, fullscreen, pointer capture, per-element events
Animation extensions The Web Animations API on any element
Keyboard App-wide keyboard shortcuts with modifier support
IntersectionObserver Element visibility inside the viewport or a scroll container
MutationObserver DOM tree, attribute and character-data mutations
ResizeObserver Element size changes with box-model detail
PictureInPicture Float a <video> in an always-on-top window
ViewTransition Animate between two states of the page, the browser doing the work
Media element extensions Play, pause, seek, volume and rate on any <audio>/<video>

Storage

Service What it wraps
LocalStorage / SessionStorage Synchronous key/value storage, per origin or per tab
Cookie Read, set and remove document cookies with full options
CookieStore The async Cookie Store API, with change events
IndexedDb Structured, transactional client-side database
CacheStorage The service-worker Cache API
StorageManager Quota, usage estimates and persistence
StorageAccess Ask for unpartitioned storage from inside a third-party iframe

Files & data

Service What it wraps
FileReader Read user-selected files as text, data URLs or bytes
FileSystem The File System Access API: pick real files/folders and write back to them
ObjectUrls Create and revoke blob: object URLs from C# data
Clipboard Read and write text and typed items on the system clipboard
Crypto SubtleCrypto: encryption, decryption, hashing, key generation, random values
Fetch The fetch API with full request/response control and progress
Compression Gzip and deflate through the browser's native codec

Network & workers

Service What it wraps
ServiceWorker Register and inspect service workers, and message them
BackgroundSync Defer work until the user has connectivity (one-shot and periodic)
Push Web push subscriptions
NetworkInformation Connection type, speed, save-data and change events
BroadcastChannel Message other tabs and windows of the same origin
WebLocks Cross-tab cooperative resource locking
EventSource Server-sent events, with reconnection built into the browser

Device & hardware

Service What it wraps
Battery Battery level, charging state and related events
Geolocation Current position, watched positions and errors
MediaDevices Cameras and microphones, plus screen capture (getDisplayMedia)
Gamepad Game controllers: buttons, sticks, triggers and rumble
DeviceOrientation Tilt, acceleration and rotation from the device's own sensors
Nfc Read and write NDEF messages on NFC tags
WakeLock Keep the screen awake, with an auto-reacquiring persistent mode
IdleDetector User and screen idle-state changes
ContactPicker Let users pick contacts to share with your app
EyeDropper Sample any pixel colour on the screen
BarcodeDetector Find QR codes and barcodes in a camera frame or an image

Identity & permissions

Service What it wraps
WebAuthn Passkeys: create credentials and verify assertions
Permissions Query the state of any browser permission
Notification Request permission and show system notifications

Media & speech

Service What it wraps
SpeechSynthesis Text-to-speech with voices, pitch and rate
SpeechRecognition Speech-to-text with interim results and events
WebAudio Play and control audio buffers
MediaRecorder Record a camera, microphone or screen share to a file
MediaSession Lock-screen metadata and hardware media-key handlers

The patterns worth knowing

Prerendering is safe by default

During static SSR / prerender there is no JS runtime. Rather than throwing, reads return a safe default - "" for strings, [] for arrays, default(T) for everything else - and void calls are no-ops. That means a read in OnInitializedAsync won't crash your prerender pass.

The trade-off: a false from IsSupported() during prerender is indistinguishable from a genuine false. If you branch on a result, do the read in OnAfterRenderAsync instead:

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender is false) return;

    if (await wakeLock.IsSupported())
    {
        await wakeLock.Request();
    }
}

Subscriptions are disposable

Anything that attaches a listener returns a ButilSubscription. Dispose it to detach:

private ButilSubscription? _subscription;

_subscription = await resizeObserver.Observe(_element, entries => { /* ... */ });

// later - idempotent, and safe during teardown:
await _subscription.DisposeAsync();

If you forget, the owning service detaches everything it registered when its scope is torn down. That's a safety net, not a plan.

Handles own hardware

MediaStreamHandle, MediaRecordingHandle, WakeLock's persistent handle and the File System handles all represent something the browser is holding open. Dispose them:

await using var stream = await mediaDevices.GetUserMedia(audio: false, video: true);
await stream!.AttachTo(_videoElement);
// the camera light goes out when the handle is disposed

Gestures and secure contexts

Many APIs only work from inside a user-gesture handler (a click), or only over HTTPS. Butil doesn't hide that - each method's XML docs say which preconditions apply, and calls that the browser refuses come back as false/null rather than as exceptions where dismissal is a normal outcome.

Optional fast invoke

On Blazor WebAssembly, the handful of APIs backed by genuinely synchronous JS functions - LocalStorage, SessionStorage, Cookie, Console, Location - can skip the async marshalling:

BitButil.UseFastInvoke();

Everything wrapping a Promise-returning API keeps running asynchronously regardless, so this can't break those calls. On Blazor Server it's a no-op.


Trimming and AOT

The package is marked IsTrimmable. Types crossing the interop boundary carry [DynamicDependency] annotations, so trimming a published WASM app keeps what the serializer needs. The public FastInvoke* extensions are annotated [RequiresUnreferencedCode] so a trimming consumer gets the warning at their own call site.

Shipping only the JavaScript you use

bit-butil.js covers every API in the package. The C# side of an unused API is trimmed away from a published app (see AddBitButilServices above); the JavaScript side can be tree-shaken too. There are two ways of tree-shaking it, both set in the app's csproj, and both working from the same per-module build of the scripts (one Scripts/*.ts file is one module, BitButil.clipboard for Clipboard and so on).

Publish-time bundle trimming - the default, nothing to add. Keep the script tag. When the app is published trimmed - a Blazor WebAssembly publish is - the package's build logic reads the trimmed Bit.Butil.dll, finds which BitButil.<module>.* identifiers survived (every interop call goes through such a literal, so the trimmed assembly is the exact list of modules the app can still reach) and replaces bit-butil.js with a bundle assembled from only those modules and their dependencies. Fingerprints, integrity hashes and compressed variants are computed from the new content. An app that injects Clipboard, LocalStorage and Window ships about 8 KB of JavaScript instead of the 110 KB bundle. It is on by default only in a Blazor WebAssembly project - a standalone app or PWA - because that is where the assembly being trimmed is the assembly calling the served JavaScript; a server that hosts a WebAssembly client keeps its own, full copy of the bundle (use lazy scripts there). The same property trims the other shape too: wherever the module files are published - a lazy-scripts app, or an app keeping both shapes - only the modules the trimmed assembly can still name are published, and the rest of the modules/ folder is dropped. Nothing can 404 over it: the identifier that would have imported a dropped module is gone from the assembly with it. <BitButilIncludeScriptModules>true</BitButilIncludeScriptModules> in the csproj publishes every module regardless, for an app that reaches them from outside its own interop calls. All of this happens in dotnet publish only: a build - and dotnet run and dotnet watch on top of it - keeps the full bundle and every module, so what you debug is never the trimmed JavaScript. Opt out with false, or opt in elsewhere with true:

<PropertyGroup>
  <BitButilTrimScripts>false</BitButilTrimScripts>
</PropertyGroup>

Lazy scripts. No script tag at all: the first call into an API import()s that API's module (_content/Bit.Butil/modules/clipboard.js for Clipboard), so only the JavaScript for the APIs the app actually calls is ever downloaded - in every hosting model, trimmed or not. Each module file is self-contained and safe to load more than once. Set the property in every project that uses Butil (a Blazor Web App's server and client both) and drop the script tag from the host page:

<PropertyGroup>
  <BitButilLazyScripts>true</BitButilLazyScripts>
</PropertyGroup>

The property also drops the bundle from the published output (and, in the default mode, the module files) and turns the mode on at runtime - through the Bit.Butil.LazyScripts runtime switch and, since runtime configuration does not reach a .NET 8 WebAssembly app, a one-line module initializer compiled into the project that calls BitButil.UseLazyScripts(). BitButilIncludeScriptBundle and BitButilIncludeScriptModules override which shape ends up in the published output when you want both. In a project that also trims (a WebAssembly publish does by default) the modules published are the ones the trimmed assembly can still import; naming BitButilIncludeScriptModules in the csproj publishes all of them.

Prefer to keep it in C#? The registration call takes the same switches:

builder.Services.AddBitButilServices(options =>
{
    options.LazyScripts = true;                  // or false to insist on the bundle
    options.ScriptModulesPath = "/cdn/butil/";   // optional, when the modules are served elsewhere
    options.FastInvoke = true;                   // optional, same as BitButil.UseFastInvoke()
});

That is equivalent to BitButil.UseLazyScripts() / BitButil.UseBundledScripts() (process-wide, last call wins) and needs no script tag either. What it cannot do is change the published output - which is why BitButilLazyScripts is the better switch wherever the csproj is an option. The default (bundle mode) publishes the bundle and drops the module files, so the C# switch on its own leaves the import()s with nothing to fetch; keep them with

<PropertyGroup>
  <BitButilIncludeScriptModules>true</BitButilIncludeScriptModules>
</PropertyGroup>

and the bundle, still published, is simply never loaded (harmless - but a PWA precaches it). Publish-time bundle trimming has no runtime counterpart at all, since it happens inside dotnet publish.

Trade-offs: lazy scripts cost one extra request the first time each module is used, and the modules' first call is necessarily asynchronous (every Butil API already is). The trimmed bundle is one request and needs no thought, but only in the project that publishes trimmed.


Samples and docs

The Bit.Butil.Demo project in this repository is a full documentation site: one page per API, with runnable samples, an API reference table, and a browser-support matrix. Run it to try any of the above in your own browser.

That same server hosts an MCP server at /mcp, so an AI agent can work against Butil's real API instead of guessing at it. Point an MCP client at https://localhost:5253/mcp; every tool is also a plain HTTP GET under /api/mcp/... if you just want to look. It offers:

  • Search across everything at once (SearchButil) - this guide, the docs pages, every public member, the browser-support matrix and the demo's sources - with the exact follow-up call on each hit. The name a task suggests is rarely the name the web platform chose, which is what this is for.
  • The exact API (GetButilApiDetails), reflected out of the shipped assembly: every service, every signature, every default argument, with the XML documentation.
  • What an API needs before it works (PlanButilFeature, for one API or the whole set a feature uses): the engines that implement it, whether it wants HTTPS, a permission prompt or a user gesture, what has to be disposed, and how it behaves under prerendering - the mistakes that compile and then do nothing.
  • Setup per hosting model (GetButilSetupGuide), as the real files of a working project.
  • The docs, this guide and the demo's source as text (GetButilDocsPage, GetButilGuideSection, GetButilSourceFile) - each of them returns the list of what it can hand out when called with no argument, and the docs index doubles as the browser-support matrix.
  • Resources (butil://guide/..., butil://api/..., butil://docs/..., butil://support) and prompts for the four common jobs: adding Butil to an app, implementing a feature with it, replacing hand-written JS interop, and debugging a call that silently does nothing.

There are seven tools, and no more: a tool description is paid for in every request of every session, so a listing is not a tool of its own, a single-item lookup is not a tool when one that takes a set already resolves each member, and nothing restates the server's instructions, which the client has had in context since initialize. Start with SearchButil. See McpController.

The site's own /mcp-server page is a working client for it: it handshakes with this server on load, lists its tools, and lets you call one and read both halves of the exchange - the JSON-RPC request, the response, and the text a model would be handed. It is the fastest way to see what a tool actually returns before wiring an agent up to it.

Every public member carries XML documentation with a link to the corresponding MDN page, so IntelliSense is the reference of record.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.6.0-pre-03 0 8/25/2026
10.6.0-pre-02 458 8/14/2026
10.6.0-pre-01 509 8/4/2026
10.5.0 1,230 7/20/2026
10.5.0-pre-10 159 7/20/2026
10.5.0-pre-09 263 7/19/2026
10.5.0-pre-08 478 7/11/2026
10.5.0-pre-07 226 7/10/2026
10.5.0-pre-06 247 7/7/2026
10.5.0-pre-05 581 6/28/2026
10.5.0-pre-04 575 6/22/2026
10.5.0-pre-02 714 6/18/2026
10.5.0-pre-01 226 6/14/2026
10.4.5 2,087 5/28/2026
10.4.5-pre-02 144 5/27/2026
10.4.5-pre-01 119 5/26/2026
10.4.4 495 5/16/2026
10.4.4-pre-04 176 5/13/2026
10.4.4-pre-03 293 5/10/2026
10.4.4-pre-02 449 4/15/2026
Loading failed