AdaskoTheBeAsT.Interop.Execution.Hosting
3.1.0
dotnet add package AdaskoTheBeAsT.Interop.Execution.Hosting --version 3.1.0
NuGet\Install-Package AdaskoTheBeAsT.Interop.Execution.Hosting -Version 3.1.0
<PackageReference Include="AdaskoTheBeAsT.Interop.Execution.Hosting" Version="3.1.0" />
<PackageVersion Include="AdaskoTheBeAsT.Interop.Execution.Hosting" Version="3.1.0" />
<PackageReference Include="AdaskoTheBeAsT.Interop.Execution.Hosting" />
paket add AdaskoTheBeAsT.Interop.Execution.Hosting --version 3.1.0
#r "nuget: AdaskoTheBeAsT.Interop.Execution.Hosting, 3.1.0"
#:package AdaskoTheBeAsT.Interop.Execution.Hosting@3.1.0
#addin nuget:?package=AdaskoTheBeAsT.Interop.Execution.Hosting&version=3.1.0
#tool nuget:?package=AdaskoTheBeAsT.Interop.Execution.Hosting&version=3.1.0
AdaskoTheBeAsT.Interop
A focused interop toolbox for dedicated-thread execution, native library isolation, and COM-friendly workloads.
New in 3.1: Use ExecutionPoolConfiguration.IsValid in your options validation
and ConfigureStaPool in the hosted pool callback. Pass your own worker limit and
keep any custom stack size, startup hooks, or deadline application in your integration.
See ADR 0021.
π¬ Code quality β SonarCloud
π Hello, interop friend
Interop code is fun, right up until it isn't. You know the signs:
- 𧬠a native library that secretly wants thread affinity
- π’ a COM component that quietly insists on STA + message pumping
- π§Έ an engine that loses its mind if two threads touch it at once
- β»οΈ a workload that needs explicit load / unload / recycle, not "hope the process survives"
AdaskoTheBeAsT.Interop.Execution is the reusable boilerplate you keep rewriting in every project: a dedicated worker thread (or a pool of them), a session it owns, a queue in front of it, and all the cancellation / disposal / telemetry plumbing that should just be a library by now. π¦
And now it is. β¨
Changelog
3.0.0
The version in Directory.Build.props is 3.0.0
across all four packages. This release upgrades the repository to .NET 11
(11.0.100) and C# 14, moves the multi-targeted matrix to net11.0,
net10.0, net481, net48, and net472 (removing net8.0 and net9.0),
introduces AdaskoTheBeAsT.Interop.Execution.AspNetCore (net11.0;net10.0),
and adds contextual session lifecycle metadata, delegate session factories,
IExecutionDispatcher<TSession> DI forwarding, validated IServiceProvider
options, typed admission-rejection exceptions, dedicated worker-thread stack
sizing and scopes, execution deadline extensions, and Hosting IHealthCheck
support:
- Contextual session lifecycle &
DelegateExecutionSessionFactory<TSession>:SessionLifecycleContext,SessionRecycleReason,ContextualExecutionSessionFactory<TSession>, andDelegateExecutionSessionFactory<TSession>pass a stableWorkerIndex(0..WorkerCount - 1),WorkerCount,WorkerName,ManagedThreadId,PreviousRecycleReason, and matchingSessionGenerationvalues to bothCreateSessionandDisposeSession, with single-lambda DI and Hosting registration overloads. - Ergonomic DI & Hosting registration:
AddExecutionWorker<TSession>andAddExecutionWorkerPool<TSession>automatically forwardIExecutionDispatcher<TSession>viaTryAddSingleton, addAction<IServiceProvider, ...>and worker-indexed overloads (mirrored onAddExecutionWorkerHostedService<TSession>andAddExecutionWorkerPoolHostedService<TSession>), and validate options on startup viaIValidateOptions<T>+.ValidateOnStart(). - Worker-thread lifecycle &
MaxStackSize: optional caller-sideBeforeThreadStartplus dedicated-threadMaxStackSize,OnWorkerThreadStart, and worker-lifetimeCreateWorkerThreadScope(IDisposabledisposed on the same worker thread strictly after finalDisposeSession()). - Typed
ExecutionAdmissionRejectedException& execution deadlines: boundedQueueCapacitysaturation throwsExecutionAdmissionRejectedException : InvalidOperationExceptioncarryingWorkerNameandQueueCapacity, whileExecutionDispatcherExtensionsbounds startup, queueing, and execution waits withTimeSpan timeout. - Hosting health checks &
Execution.AspNetCoreintegration: extendedExecutionWorkerSnapshot/ExecutionWorkerPoolSnapshotreadiness fields, built-inIHealthCheckregistrations inExecution.Hosting, and a newAdaskoTheBeAsT.Interop.Execution.AspNetCorecompanion package (net11.0;net10.0) providingWithExecutionExceptionHandling,AddExecutionExceptionHandler, AOT-safe source-generated diagnostics DTOs (MapExecutionWorkerDiagnostics,MapExecutionWorkerPoolDiagnostics), andMapExecutionHealthProbes. - Target framework & dependency updates: added
net11.0, removednet8.0andnet9.0, updatednet11.0and.NET Frameworkpackage ranges to[11.0.0, 12.0.0)andnet10.0Microsoft.Extensions.*ranges to[10.0.15, 11.0.0), and updated SDK (11.0.100), analyzer, andxunit.v3(4.0.3) packages.
See the 2.x to 3.0.0 migration guide and the full changelog for release history.
Why upgrade to 2.0.0?
2.0.0 makes worker execution and shutdown more reliable and raises the minimum
.NET Framework version to 4.7.2. Dropping
net462, net47, and net471 is a breaking change and the reason for the major
version. Existing integration APIs remain available on supported targets.
The comparison below is against the tagged 1.0.0 release.
| If your application... | What could go wrong in 1.0.0 | What 2.0.0 improves |
|---|---|---|
Uses ExecuteValueAsync under load |
A caller could return a pooled source while the worker still used it; delegate failures bypassed worker recovery. | Completion is the worker's last access to the item. Task and ValueTask requests share failure, cancellation, recycling, and outcome reporting. |
| Recycles native sessions | A request could report success before required teardown finished. | Awaiting the request includes recycle teardown. Cleanup failures become observable instead of looking like successful work. |
| Submits work before initialization finishes | An async submission could block its calling thread on session creation. | Submissions enqueue without waiting for creation and retain per-worker FIFO order. Startup failures settle the returned requests. |
| Shuts down from several places | A repeated or reentrant disposal could make an external caller think teardown was already complete. | Every external DisposeAsync() joins actual teardown, even after a synchronous disposal timeout. |
| Receives bursts of work | Admission was unbounded. | Opt into QueueCapacity to limit waiting requests and reject excess work explicitly, including during startup. |
| Needs a shutdown policy | Pending-work handling was not an explicit choice. | Choose Drain or CancelPending. Hosted shutdown also observes the host's cancellation deadline without canceling cleanup. |
| Relies on faults and tracing | Fault handlers ran on the owning thread before Fault was published; listener errors could disrupt execution. |
Fault state is latched before off-thread notification. Listener exceptions are contained, and spans use the submitting request's activity context. |
| Reuses options or custom pool schedulers | Later option mutations could alter live behavior; a scheduler could select a foreign concrete worker. | Workers snapshot configuration, pools reject non-members, and partial pool construction rolls back earlier workers. |
What is not new: pooled ExecuteValueAsync, DI/Hosting,
session recycling, schedulers, snapshots, and scoped diagnostics already shipped
in 1.0.0. This is a correctness and lifecycle upgrade, not a claim of faster
native execution or a new benchmark result.
Before upgrading: applications targeting .NET Framework 4.6.2, 4.7, or 4.7.1 must retarget to 4.7.2 or later, or remain on 1.0.0. Modern .NET 8, 9, and 10 targets are unchanged.
On supported targets, existing public signatures are retained. The new options
default to unlimited admission (QueueCapacity = 0) and Drain; applications
receive the fixes without opting into queue limits. Completion timing, event
ordering, and option validation do change, so review the
1.0.x to 2.0.0 migration guide before upgrading.
See the changelog for release details.
β¨ Why you'll love this
- Pooled ValueTask path.
ExecuteValueAsyncreusesIValueTaskSource<T>work items on every supported TFM, includingnet472, avoiding a per-request source/Task allocation when a pooled item is available. This is not a guarantee of zero total allocations. (Completion contract) - DI + Hosting. Use
AddExecutionWorker<TSession>()for plain DI, orAddExecutionWorkerHostedService<TSession>()to register both the worker and its hosted lifecycle. - π« Pluggable schedulers.
LeastQueuedandRoundRobinship in the box; bring your own viaIWorkerScheduler<TSession>. (ADR-0002) - π Batteries-included observability.
ActivitySource+Meterwith public constant names, ready for OpenTelemetry. (ADR-0003) - πͺ First-class Windows STA. Flip a boolean, get an STA worker thread on Windows; silently ignored elsewhere.
- β»οΈ Real session recycling. After N operations, after a failure, or both β your call.
- π‘οΈ Terminal-once faulting. When a worker goes bad, it says so once, loudly, via
WorkerFaultedβ no silent-dead-thread surprises. - β±οΈ Built-in execution deadlines & health checks. Bound caller waits with
TimeSpanextensions onIExecutionDispatcher<TSession>and wire readiness/degradation checks straight intoAddHealthChecks(). - 5 target frameworks.
net11.0,net10.0,net481,net48,net472. .NET Framework 4.7.2 is the desktop minimum and .NET 10 is the modern .NET minimum in 3.0.0. - βοΈ Source Link + snupkg. Step into the library from your debugger without guessing.
π¦ Packages
| Package | What it gives you |
|---|---|
AdaskoTheBeAsT.Interop.Execution |
β Core: ExecutionWorker<TSession>, ExecutionWorkerPool<TSession>, IExecutionSessionFactory<TSession>, IContextualExecutionSessionFactory<TSession>, DelegateExecutionSessionFactory<TSession>, ExecutionAdmissionRejectedException, deadline extensions, options, schedulers, diagnostics. |
AdaskoTheBeAsT.Interop.Execution.DependencyInjection |
π§© Microsoft.Extensions.DependencyInjection helpers: AddExecutionWorker<TSession>() / AddExecutionWorkerPool<TSession>() with automatic IExecutionDispatcher<TSession> forwarding, inline session delegates, worker-indexed resolvers, and ValidateOnStart IOptions<T> binding. |
AdaskoTheBeAsT.Interop.Execution.Hosting |
ποΈ Microsoft.Extensions.Hosting & HealthChecks integration: IHostedService wrappers and IHealthCheck registrations driving worker / pool lifetime and readiness from the generic host. |
AdaskoTheBeAsT.Interop.Execution.AspNetCore |
π ASP.NET Core Minimal API & ProblemDetails integration (net11.0;net10.0): WithExecutionExceptionHandling, AddExecutionExceptionHandler, AOT-safe source-generated diagnostics DTOs (MapExecutionWorkerDiagnostics, MapExecutionWorkerPoolDiagnostics), and MapExecutionHealthProbes. |
β¬οΈ Install
dotnet add package AdaskoTheBeAsT.Interop.Execution
dotnet add package AdaskoTheBeAsT.Interop.Execution.DependencyInjection
dotnet add package AdaskoTheBeAsT.Interop.Execution.Hosting
dotnet add package AdaskoTheBeAsT.Interop.Execution.AspNetCore
Symbols ship as .snupkg with Source Link and embedded untracked sources. Step in. Look around. It's fine.
πΊοΈ Target framework matrix
| TFM | Status | Notes |
|---|---|---|
net11.0 |
β | Primary target across all 4 packages; in-box System.Threading.Channels + System.Diagnostics.DiagnosticSource. |
net10.0 |
β | Primary LTS target across all 4 packages. |
net481 |
β | Windows desktop (Execution, DependencyInjection, Hosting); System.Threading.Channels + System.Diagnostics.DiagnosticSource via NuGet + IsExternalInit polyfill. |
net48 |
β | Same as above. |
net472 |
β | Same as above. |
The five-target matrix applies to Execution, Execution.DependencyInjection,
and Execution.Hosting, while Execution.AspNetCore targets net11.0 and
net10.0 (Microsoft.AspNetCore.App).
Removed in 3.0.0: net8.0 (.NET 8) and net9.0 (.NET 9); see the
2.x to 3.0.0 migration guide.
Removed in 2.0.0: net462 (.NET Framework 4.6.2), net47 (4.7), and
net471 (4.7.1); see the 1.0.x to 2.0.0 migration guide.
CI enables TreatWarningsAsErrors=true, ContinuousIntegrationBuild=true, and
Deterministic=true. The four multi-targeted test projects target this same
five-TFM matrix, while Execution.AspNetCore.Test targets net11.0 and
net10.0.
π‘ The core idea
Instead of every interop-heavy engine reinventing:
- a queue π
- a worker thread π§΅
- startup synchronisation π
- session lifetime β³
- disposal logic ποΈ
- failure / recycle behaviour β»οΈ
...you park that generic machinery in ExecutionWorker<TSession> or ExecutionWorkerPool<TSession>. Your engine becomes a thin adapter that answers three questions:
- π± How do I create a session?
- π₯ How do I dispose a session?
- π οΈ What work should run on that session?
That's it. The rest is the library's problem now.
ββββββββββββββββββββββββββββββββ
ExecuteAsync(x) βββΆ β Channel<ExecutionWorkItem> β
β (multi-writer, 1 reader) β
ββββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Dedicated Thread β
β owns ONE TSession β βββ STA on Windows
β runs work in FIFO β if you ask
βββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββ
β TSession β (native libs, COM, ...)
βββββββββββββββ
π Main types
βοΈ ExecutionWorker<TSession>
A single dedicated background thread that owns one TSession. Submitted work runs sequentially in FIFO order. Implements IDisposable and IAsyncDisposable.
Owns π
- a multi-writer / single-reader
Channelof work items - one dedicated background
Thread(optionally STA on Windows) - startup / shutdown lifecycle (
InitializeAsync(CancellationToken)+ syncInitialize) - configurable draining or cancellation of pending items on shutdown
- session reuse + session recycle after failure or after N operations
- observability via
Name,IsFaulted,Fault,QueueDepth,WorkerFaulted, and the uniformGetSnapshot()
βοΈβοΈβοΈβοΈ ExecutionWorkerPool<TSession>
Fan-out pool of ExecutionWorker<TSession> instances. Each pool worker owns a private session and a private queue; a pluggable IWorkerScheduler<TSession> picks which worker receives each submission.
Owns π
- multiple
ExecutionWorker<TSession>instances - pluggable work distribution (see Scheduling below)
- one session per worker (ideal for isolated native DLL sets)
- separate worker-owned sessions (process-global native state still needs adapter-level isolation)
- parallel initialization and parallel disposal
- aggregate observability (
QueueDepth,IsAnyFaulted,WorkerFaults, forwardedWorkerFaulted, and per-worker snapshots viaGetSnapshot().Workers)
π IExecutionSessionFactory<TSession>, IContextualExecutionSessionFactory<TSession>, and DelegateExecutionSessionFactory<TSession>
public interface IExecutionSessionFactory<TSession>
where TSession : class
{
TSession CreateSession(CancellationToken cancellationToken);
void DisposeSession(TSession session);
}
Creates the thread-affine session (loading native libs, initialising modules) and disposes / unloads it. Both methods run on the dedicated worker thread.
Need the stable worker slot (0..WorkerCount - 1), pool size, session generation (0, 1, 2, ...), or previous recycle reason (MaxOperationsReached / OperationFaulted)? Implement IContextualExecutionSessionFactory<TSession> (or subclass ContextualExecutionSessionFactory<TSession>): CreateSession and DisposeSession receive the same SessionLifecycleContext.SessionGeneration for a given session lifetime.
Don't want to write a custom factory class just to invoke a constructor and Dispose()? Use DelegateExecutionSessionFactory<TSession> (or the inline delegate overloads in DI and Hosting):
var factory = new DelegateExecutionSessionFactory<NativeSession>(
(context, ct) => new NativeSession(context.WorkerIndex));
// Automatically checks ct.ThrowIfCancellationRequested() and calls (session as IDisposable)?.Dispose().
ποΈ ExecutionWorkerOptions
Name, UseStaThread, MaxStackSize (0 = runtime default, > 0 = dedicated thread stack reservation in bytes), MaxOperationsPerSession (0 = unlimited), DisposeTimeout (default Timeout.InfiniteTimeSpan), Diagnostics (scoped ExecutionDiagnostics instance β defaults to a process-wide Shared singleton), BeforeThreadStart (optional callback invoked synchronously on the caller before creating and starting the worker thread), OnWorkerThreadStart (invoked once on the dedicated worker thread after ApartmentState is set and before initial CreateSession), and CreateWorkerThreadScope (Func<IDisposable?>? invoked once on the dedicated worker thread before initial CreateSession, surviving session recyclings, and disposed on that exact worker thread strictly after the final DisposeSession()). Parameterless ctor + positional ctor + public setters + public Validate() so it binds cleanly via IOptions<T>.
QueueCapacity limits requests waiting for startup or execution (0 = unlimited).
ShutdownMode selects Drain (default) or CancelPending. Options are validated
and copied when the worker is constructed; later mutations do not reconfigure it.
ποΈ ExecutionWorkerPoolOptions
WorkerCount, Name, UseStaThread, MaxStackSize, MaxOperationsPerSession, DisposeTimeout, SchedulingStrategy (default LeastQueued), Diagnostics, BeforeThreadStart (invoked synchronously once per pool startup attempt before any worker thread is created), OnWorkerThreadStart (Action<int>? receiving workerIndex), and CreateWorkerThreadScope (Func<int, IDisposable?>? receiving workerIndex). Same binding and Validate() story.
QueueCapacity and ShutdownMode apply to each worker. Pool options are also
copied at construction. Capacity is per worker, not a shared pool-wide limit.
ποΈ ExecutionRequestOptions
Per-call knob: RecycleSessionOnFailure (default false).
πͺ STA behavior
If UseStaThread: true is set:
- β
On Windows, the worker thread is configured as
STAviaSetApartmentState(ApartmentState.STA)(guarded byOperatingSystem.IsWindows()onnet5+andPlatformID.Win32NTon older TFMs). - π€· On non-Windows, the flag is silently ignored.
That makes the option safe for cross-platform callers that want "STA when possible" behaviour.
The worker does not run a COM or UI message pump. STA alone is insufficient for components that require one. Separate worker sessions also do not isolate process-global native state; the adapter must supply any required process-wide serialization or process isolation.
Startup, admission, cancellation, and shutdown
- Submit synchronous delegates only. An async lambda can escape the owning thread and outlive its session. Do not synchronously wait for nested work on the same worker.
- Async submissions return without waiting for initial session creation. Work is queued in FIFO order on each worker, including during startup.
InitializeAsync(token)cancels only that caller's wait. Shared startup continues for other callers. Disposing the worker separately requests cancellation of initial session creation.- A full
QueueCapacityfaults the submission withExecutionAdmissionRejectedException(which derives fromInvalidOperationExceptionand exposesWorkerNameandQueueCapacity); it does not block or silently drop work. The executing request is excluded from the limit. A pool does not retry another worker after capacity rejection. - A request canceled before execution is skipped when dequeued. Once running, its delegate must observe its own token. Neither request cancellation nor shutdown forcibly interrupts managed or native code.
Draincompletes queued work on an available session.CancelPendingskips requests not yet started. Both let running delegates finish before teardown. If initial creation is canceled or fails, queued requests cannot run and are completed with cancellation or failure.- Every external
DisposeAsync()joins the same actual teardown. A call from the owning worker only requests shutdown, avoiding a self-deadlock. SynchronousDispose()can abandon its wait atDisposeTimeout; cleanup continues and a laterDisposeAsync()still joins it. - Hosted
StopAsync(token)uses the host deadline to bound its wait, not the cleanup itself. If the token fires before teardown completes, awaitingStopAsyncthrowsOperationCanceledException. Container disposal may still wait for a blocked native call.
Hard deadlines and recovery from a hung native call require a separate process. See ADR-0010 for the ownership and compatibility decisions.
π Quick example
This complete console example uses top-level statements on modern .NET. The
session is a stub; replace Render and the factory cleanup with your native API.
using System.Text;
using AdaskoTheBeAsT.Interop.Execution;
CancellationToken cancellationToken = CancellationToken.None;
// 1. Spin up the worker.
await using var worker = new ExecutionWorker<NativeSession>(
new NativeSessionFactory(),
new ExecutionWorkerOptions(
name: "Native Render Worker",
useStaThread: true,
maxOperationsPerSession: 500));
await worker.InitializeAsync(cancellationToken);
// 2. Throw work at it. Returns when the work item completes.
byte[] bytes = await worker.ExecuteAsync(
(session, ct) =>
{
ct.ThrowIfCancellationRequested();
return session.Render("<h1>Hello</h1>");
},
new ExecutionRequestOptions(recycleSessionOnFailure: true),
cancellationToken);
Console.WriteLine($"Produced {bytes.Length} bytes.");
public sealed class NativeSession
{
public byte[] Render(string html) => Encoding.UTF8.GetBytes(html);
}
public sealed class NativeSessionFactory : IExecutionSessionFactory<NativeSession>
{
public NativeSession CreateSession(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return new NativeSession();
}
public void DisposeSession(NativeSession session)
{
// Free native handles here, on the owning worker thread.
}
}
Pooled ValueTask hot path
Both the IExecutionWorker<TSession> and IExecutionWorkerPool<TSession>
interfaces and the concrete ExecutionWorker<TSession> and
ExecutionWorkerPool<TSession> types expose instance ExecuteValueAsync
overloads backed by pooled IValueTaskSource<T> work items on every supported
TFM. Use the same synchronous delegate as with ExecuteAsync; only the returned
completion type changes.
byte[] bytes = await worker.ExecuteValueAsync(
(session, ct) =>
{
ct.ThrowIfCancellationRequested();
return session.Render("<h1>Hello</h1>");
},
cancellationToken: cancellationToken);
Await each returned ValueTask exactly once. If you need to share or repeatedly
await a result, use ExecuteAsync, or call AsTask() once and retain that Task.
Do not discard, double-await, or synchronously read an incomplete ValueTask.
Pooling avoids per-request source/Task allocations when an item can be reused; startup, pool misses, delegates, channels, continuations, and tracing can still allocate. The pooling API already existed in 1.0.0. The 2.0.0 improvement is safe completion ownership and the same recovery behavior as the Task path. See ADR-0010, which supersedes the lifecycle design in ADR-0007.
Execution deadlines (TimeSpan timeout)
ExecutionDispatcherExtensions provides ExecuteAsync and ExecuteValueAsync
extension overloads with a TimeSpan timeout on IExecutionDispatcher<TSession>:
byte[] bytes = await worker.ExecuteAsync(
(session, ct) =>
{
ct.ThrowIfCancellationRequested();
return session.Render("<h1>Hello</h1>");
},
timeout: TimeSpan.FromSeconds(2),
cancellationToken: cancellationToken);
The deadline bounds startup, queueing, and execution. Queued work that expires
before dequeue never runs; deadline expiry throws TimeoutException, while
caller cancellation throws OperationCanceledException with the caller's exact
CancellationToken. If a synchronous native call is already running when the
deadline fires, the caller returns promptly while the worker stays occupied
until the native call completes.
ππππ Pool: multiple workers
If one worker is not enough, use ExecutionWorkerPool<TSession>. Great fit when:
- π you have several native library copies in separate folders
- π¦ each worker should load its own isolated DLL set
- β»οΈ one failed worker session should recycle without touching the others
- ποΈ you want several dedicated threads, but still serialised execution per worker
await using var pool = new ExecutionWorkerPool<NativePoolSession>(
workerIndex => new NativePoolSessionFactory($@"c:\native\slot-{workerIndex + 1:D2}"),
new ExecutionWorkerPoolOptions(
workerCount: 4,
name: "Native Pool",
useStaThread: true,
maxOperationsPerSession: 250));
await pool.InitializeAsync(cancellationToken);
var result = await pool.ExecuteAsync(
(session, ct) => session.Render("<h1>Hello from pool</h1>"),
new ExecutionRequestOptions(recycleSessionOnFailure: true),
cancellationToken);
π‘ Tip: need every worker to share the exact same factory? There's a single-factory constructor overload too:
new ExecutionWorkerPool<T>(factory, options). Nice and tidy for stateless factories. (ADR-0008)
π¦ Scheduling
The pool ships with two built-in schedulers and a public IWorkerScheduler<TSession> seam if you need something bespoke.
| Built-in | Icon | Semantics |
|---|---|---|
LeastQueuedWorkerScheduler<TSession> (default) |
βοΈ | Picks the healthy worker with the smallest QueueDepth. Ties break via a shared rolling index so equal-depth workers rotate. Skips faulted workers. Early-exits when it finds a zero-depth worker. |
RoundRobinWorkerScheduler<TSession> |
π | Strict rotation across healthy workers via an Interlocked index. Skips faulted workers. |
Swap built-ins via options, or plug in a custom scheduler via the pool ctor:
// Option A: pick a built-in via options.
var opts = new ExecutionWorkerPoolOptions(
workerCount: 4,
schedulingStrategy: SchedulingStrategy.RoundRobin);
// Option B: inject a custom scheduler.
IWorkerScheduler<NativeSession> custom = new MyAffinityScheduler();
await using var pool = new ExecutionWorkerPool<NativeSession>(
workerIndex => new NativeSessionFactory(),
new ExecutionWorkerPoolOptions(workerCount: 4),
custom);
Rationale, trade-offs, and the faulted-worker contract are captured in ADR-0002.
π€ When to use what
1οΈβ£ Choose ExecutionWorker<TSession> when
- π the native engine is effectively process-global
- π₯΅ the library is known to be thread-sensitive
- β you want strict serialised access to one engine instance
- π you want exactly one owner thread
4οΈβ£ Choose ExecutionWorkerPool<TSession> when
- π€π€π€π€ you have isolated native copies per worker
- ποΈποΈ the library can run in parallel across separate worker-owned sessions
- π you want better throughput
- π§ you want one worker to recycle independently from the others
β»οΈ Session recycle story
You can choose to recycle the session:
- β after a failed request β
ExecutionRequestOptions.RecycleSessionOnFailure = true - π― after a fixed number of operations β
ExecutionWorkerOptions.MaxOperationsPerSession > 0 - β¨ or both
Set maxOperationsPerSession: 0 when you want unlimited session lifetime and only failure-based recycling.
Task and ValueTask calls use the same failure and recycle policy. Completion
is published only after any required session teardown. A teardown error after
a successful delegate faults the request and worker. If the delegate also
failed, its exception remains the request failure and Fault records the
terminal teardown error. A canceled request does not count as a successful
operation or trigger failure-based recycling.
π§© DI integration
using AdaskoTheBeAsT.Interop.Execution;
using AdaskoTheBeAsT.Interop.Execution.DependencyInjection;
// Option A: class-based session factory
services.AddSingleton<IExecutionSessionFactory<NativeSession>, NativeSessionFactory>();
services.AddExecutionWorker<NativeSession>(options =>
{
options.Name = "Native Render Worker";
options.UseStaThread = true;
options.MaxOperationsPerSession = 500;
});
// Option B: inline session delegate + IServiceProvider-aware options (validated on start)
services.AddExecutionWorkerPool<NativeSession>(
(sp, ctx, ct) => new NativeSession(ctx.WorkerIndex),
(sp, options) =>
{
var cfg = sp.GetRequiredService<IOptions<HostPoolSettings>>().Value;
options.WorkerCount = cfg.WorkerCount;
options.QueueCapacity = cfg.QueueCapacity;
});
// Resolve IExecutionDispatcher<NativeSession>, IExecutionWorker<NativeSession>,
// or IExecutionWorkerPool<NativeSession> from DI and use it as usual.
Both AddExecutionWorker<TSession> and AddExecutionWorkerPool<TSession> automatically forward IExecutionDispatcher<TSession> via TryAddSingleton and wire IValidateOptions<T> + .ValidateOnStart() so invalid option values fail fast at host startup. AddExecutionWorkerPool<TSession> resolves IContextualExecutionSessionFactory<TSession> when registered (falling back to IExecutionSessionFactory<TSession>), resolves an optional IWorkerScheduler<TSession>, and also offers worker-indexed ((sp, workerIndex) => ...) and inline-delegate ((sp, ctx, ct) => ...) overloads.
ποΈ Generic host & health-check integration
using AdaskoTheBeAsT.Interop.Execution.Hosting;
services.AddSingleton<IExecutionSessionFactory<NativeSession>, NativeSessionFactory>();
services.AddExecutionWorkerHostedService<NativeSession>(options =>
{
options.Name = "Native Render Worker";
options.UseStaThread = true;
});
services.AddHealthChecks()
.AddExecutionWorkerHealthCheck<NativeSession>(
configure: health => health.QueueDepthWarningThreshold = 16,
tags: ["ready"]);
The registration includes the worker, IExecutionDispatcher<TSession> forwarding,
and its IHostedService wrapper; you do not need a separate
AddExecutionWorker<TSession>() call. Both DI and Hosting also support inline
createSession delegates (DelegateExecutionSessionFactory<TSession>) and
Action<IServiceProvider, ...> option callbacks with .ValidateOnStart(). The
wrapper drives InitializeAsync on StartAsync and joins DisposeAsync on
StopAsync.
In 2.0.0+, a canceled stop token cancels only that wait, not worker cleanup.
Repeated stops join the same teardown, subject to each caller's token.
AddExecutionWorkerPoolHostedService<TSession> and
AddExecutionWorkerPoolHealthCheck<TSession> cover the pool (reporting
Unhealthy before eager initialization completes, after disposal, or when all
workers fault, and Degraded on partial worker loss or elevated queue depth).
π ASP.NET Core Minimal API integration
using AdaskoTheBeAsT.Interop.Execution.AspNetCore;
using AdaskoTheBeAsT.Interop.Execution.Hosting;
builder.Services.AddExecutionAspNetCoreJson();
builder.Services.AddExecutionWorkerPoolHostedService<NativeSession>(
(sp, ctx, ct) => new NativeSession(ctx.WorkerIndex),
(sp, options) =>
{
options.WorkerCount = 4;
options.QueueCapacity = 32;
});
builder.Services.AddHealthChecks()
.AddExecutionWorkerPoolHealthCheck<NativeSession>(tags: ["ready"]);
var app = builder.Build();
app.MapExecutionHealthProbes(); // /healthz + /readyz
app.MapExecutionWorkerPoolDiagnostics<NativeSession>(); // AOT-safe /api/diagnostics
app.MapPost("/api/calculate", async (
CalcRequest req,
IExecutionDispatcher<NativeSession> dispatcher,
CancellationToken ct) =>
TypedResults.Ok(await dispatcher.ExecuteValueAsync(
(session, token) => session.Calculate(req, token),
TimeSpan.FromSeconds(5),
ct)))
.WithExecutionExceptionHandling("Native engine unavailable.");
WithExecutionExceptionHandling (available on both RouteHandlerBuilder and
RouteGroupBuilder, alongside the optional global
builder.Services.AddExecutionExceptionHandler(...) + app.UseExceptionHandler()
pipeline) maps:
ArgumentExceptionβ400 Bad RequestProblemDetailsusingExecutionAspNetCoreOptions.BadRequestTitle(never echoing internal native paths or stack traces).OperationCanceledExceptionwhenHttpContext.RequestAbortedis canceled β499 Client Closed Request.TimeoutException,ExecutionAdmissionRejectedException, orObjectDisposedExceptionβ503 Service UnavailableProblemDetailsusingExecutionAspNetCoreOptions.UnavailableTitleand an optional ceiling-roundedRetry-Afterheader (ExecutionAspNetCoreOptions.RetryAfter).
MapExecutionWorkerDiagnostics<TSession> and
MapExecutionWorkerPoolDiagnostics<TSession> project snapshots through
source-generated DTOs (ExecutionWorkerDiagnosticsDto /
ExecutionWorkerPoolDiagnosticsDto backed by
ExecutionAspNetCoreJsonSerializerContext, exposing faultType instead of raw
System.Exception) so /api/diagnostics remains Native-AOT-safe and leak-free
even when workers fault. MapExecutionHealthProbes wires /healthz (zero-check
liveness) and /readyz ("ready"-tagged readiness) in a single call.
π Observability
Every worker emits to an ActivitySource and Meter named AdaskoTheBeAsT.Interop.Execution (customisable per worker via ExecutionWorkerOptions.Diagnostics β see ADR-0009 for scoped emitters).
| Instrument | Kind | Tags |
|---|---|---|
ExecutionWorker.Execute |
π Activity (span) |
worker.name |
execution.worker.operations |
π Counter<long> |
worker.name, outcome β success / faulted / cancelled |
execution.worker.session_recycles |
π Counter<long> |
worker.name, reason β max_operations / failure |
execution.worker.queue_depth |
π ObservableGauge<int> |
worker.name |
All these identifiers are exposed as public const string on ExecutionDiagnosticNames, so telemetry pipelines can subscribe without hard-coding strings:
using AdaskoTheBeAsT.Interop.Execution;
using OpenTelemetry.Trace;
using OpenTelemetry.Metrics;
builder.Services.AddOpenTelemetry()
.WithTracing(t => t.AddSource(ExecutionDiagnosticNames.SourceName))
.WithMetrics(m => m.AddMeter(ExecutionDiagnosticNames.SourceName));
When no activity listener is attached, StartActivity returns null and no
execution span is created. This does not guarantee allocation-free execution.
See ADR-0003 for why the identifiers are a public contract.
β οΈ Faulting semantics
ExecutionWorker<TSession> is terminal-once: a session creation or teardown
failure latches Fault before WorkerFaulted is queued to the thread pool.
Subscribers run off the owning thread; throwing subscribers are contained.
Notification is asynchronous and may arrive after disposal completes. The first
terminal exception wins, and the worker cannot be re-initialised.
A delegate exception alone faults that request, optionally recycling its session.
An OperationCanceledException is cancellation only when the request token was
canceled; otherwise it is a failure for both Task and ValueTask calls. Future
submissions rethrow a terminal fault synchronously unless disposal has already
been requested, in which case they throw ObjectDisposedException.
Disposal joins teardown without rethrowing terminal session errors. Inspect
Fault, IsFaulted, or WorkerFaulted for those errors. Diagnostic-listener
exceptions do not fail requests; execution spans use the submitting activity
as their parent.
Pool consumers observe the same contract aggregated: IsAnyFaulted, WorkerFaults, and a forwarded WorkerFaulted event carrying the originating worker name. π
π Migrating
- From 2.x to 3.0.0: see the 3.0.0 migration guide. Retarget any
net8.0ornet9.0projects tonet10.0ornet11.0; onnet11.0,net10.0,net481,net48, andnet472, existing2.2.0APIs remain source- and binary-compatible. - From 1.0.x to 2.0.0: see the 2.0.0 migration guide. It covers retargeting from
.NET Framework < 4.7.2, before/after completion and fault ordering, option snapshots,QueueCapacity, andShutdownMode.
π§ͺ Build and test
dotnet build .\AdaskoTheBeAsT.Interop.slnx
dotnet test --solution .\AdaskoTheBeAsT.Interop.slnx --no-build
| Project | Role |
|---|---|
test/unit/AdaskoTheBeAsT.Interop.Execution.Test |
π¬ Unit + behavioural (fault propagation, dispose idempotency, cancellation, deadlines, contextual & delegate factories, ExecutionAdmissionRejectedException, BeforeThreadStart, MaxStackSize, OnWorkerThreadStart, CreateWorkerThreadScope, telemetry smoke, scheduler contract). |
test/unit/AdaskoTheBeAsT.Interop.Execution.DependencyInjection.Test |
π§© DI registration, IExecutionDispatcher<TSession> forwarding, IServiceProvider options + ValidateOnStart, contextual/indexed/delegate factory resolution. |
test/unit/AdaskoTheBeAsT.Interop.Execution.Hosting.Test |
ποΈ IHostedService start/stop lifecycle, idempotent registration/shutdown, startup option validation, worker/pool IHealthCheck transitions. |
test/unit/AdaskoTheBeAsT.Interop.Execution.AspNetCore.Test |
π Minimal API TestServer filter/exception-handler status mappings (200/400/499/503), strict source-generated /api/diagnostics under faults, and /healthz + /readyz probes (net11.0;net10.0). |
test/integ/AdaskoTheBeAsT.Interop.Execution.IntegrationTest |
π€ Multi-threaded submission, STA on Windows, reentrant dispose, multi-pool contextual session recycling, zero-alloc ValueTask, snapshot surface, scoped diagnostics. |
The four core test projects target net11.0, net10.0, net481, net48, and net472, and Execution.AspNetCore.Test targets net11.0 and net10.0.
π Architecture Decision Records
Small, self-contained design decisions taken on this codebase live under docs/adr/. Start with the index. Highlights:
- π§ ADR-0002 β pluggable worker scheduler
- π·οΈ ADR-0003 β public diagnostic constants
- β‘ ADR-0007 β zero-allocation
ExecuteValueAsync - πΈ ADR-0008 β uniform snapshot surface
- π ADR-0009 β scoped
ExecutionDiagnostics - ADR-0010: 2.0.0 completion ownership and lifecycle
- π― ADR-0011 β .NET 11 target framework matrix and 3.0.0 baseline
- πͺͺ ADR-0012 β contextual session lifecycle and indexed DI/Hosting
- π¦ ADR-0013 β
BeforeThreadStartstartup ordering hook - β±οΈ ADR-0014 β execution deadline extension overloads
- π©Ί ADR-0015 β Hosting
IHealthCheckand readiness snapshots - π« ADR-0016 β typed
ExecutionAdmissionRejectedException - π ADR-0017 β
IExecutionDispatcherforwarding and validated DI options - π§© ADR-0018 β
DelegateExecutionSessionFactoryand inline delegates - π ADR-0019 β
Execution.AspNetCoreand AOT-safe diagnostics - π§΅ ADR-0020 β worker
MaxStackSizeand thread-affine scopes
π Contributing
Found a bug? Got an idea? Spotted a typo that's been haunting you? π»
- π Open an issue describing the problem or the proposal.
- π οΈ Fork + branch (
feature/your-idea). - β
Run
dotnet build+dotnet testacross the full matrix. - β¨ Add/update tests and an ADR if the change is load-bearing.
- π Open a PR β the strict-build + CI will do the rest.
π Further reading
- π
wkhtml.mdβ WkHtml migration notes. - π
docs/adr/β design rationale for every recent change. - π
CHANGELOG.mdβ what landed when. - 2.x to 3.0.0 migration guide:
.NET 11retargeting and3.0.0feature adoption. - 1.0.x to 2.0.0 migration guide: retargeting, upgrade steps, and behavioral changes.
<p align="center"> Built for the kind of interop code that likes <strong>one owner thread</strong>, <strong>explicit lifecycle</strong>, and <strong>zero drama</strong>. β¨<br/> Made with β€οΈ (and a lot of coffee β) by <a href="https://github.com/AdaskoTheBeAsT">AdaskoTheBeAsT</a>. </p>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net11.0 is compatible. |
| .NET Framework | net472 is compatible. net48 is compatible. net481 is compatible. |
-
.NETFramework 4.7.2
- AdaskoTheBeAsT.Interop.Execution (>= 3.1.0)
- AdaskoTheBeAsT.Interop.Execution.DependencyInjection (>= 3.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 11.0.0 && < 12.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 11.0.0 && < 12.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 11.0.0 && < 12.0.0)
-
.NETFramework 4.8
- AdaskoTheBeAsT.Interop.Execution (>= 3.1.0)
- AdaskoTheBeAsT.Interop.Execution.DependencyInjection (>= 3.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 11.0.0 && < 12.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 11.0.0 && < 12.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 11.0.0 && < 12.0.0)
-
.NETFramework 4.8.1
- AdaskoTheBeAsT.Interop.Execution (>= 3.1.0)
- AdaskoTheBeAsT.Interop.Execution.DependencyInjection (>= 3.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 11.0.0 && < 12.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 11.0.0 && < 12.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 11.0.0 && < 12.0.0)
-
net10.0
- AdaskoTheBeAsT.Interop.Execution (>= 3.1.0)
- AdaskoTheBeAsT.Interop.Execution.DependencyInjection (>= 3.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15 && < 11.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.15 && < 11.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.15 && < 11.0.0)
-
net11.0
- AdaskoTheBeAsT.Interop.Execution (>= 3.1.0)
- AdaskoTheBeAsT.Interop.Execution.DependencyInjection (>= 3.1.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 11.0.0 && < 12.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on AdaskoTheBeAsT.Interop.Execution.Hosting:
| Package | Downloads |
|---|---|
|
AdaskoTheBeAsT.WkHtmlToX.Hosting
Microsoft.Extensions.Hosting integration for AdaskoTheBeAsT.WkHtmlToX: AddWkHtmlToXHostedService() registers an IHostedService that drives the WkHtmlToX engine worker lifecycle through the generic host. |
|
|
AdaskoTheBeAsT.Interop.Execution.AspNetCore
ASP.NET Core minimal-API and diagnostics integration for AdaskoTheBeAsT.Interop.Execution: endpoint filters and IExceptionHandler mapping execution timeouts, capacity rejections, disposal, client disconnects, and argument errors to ProblemDetails, AOT-safe leak-free worker/pool diagnostics DTOs and JsonSerializerContext, and /healthz + /readyz probe mapping. Targets net11.0 and net10.0. |
|
|
AdaskoTheBeAsT.YaraX.Interop
Managed .NET native interop wrapper (MIT) with bundled YARA-X native libraries (BSD 3-Clause), zero-downtime hot-reloadable rules provider, hosted thread-affine scanning worker pool, and YARA Forge rule sync (Windows x64, Linux glibc x64, Alpine musl x64). |
GitHub repositories
This package is not used by any popular GitHub repositories.
## [3.1.0]
Adds a shared hosted STA pool configuration and validation helper without changing existing worker defaults.