NetworkInspector.Plugins.Loader
0.23.0
See the version list below for details.
dotnet add package NetworkInspector.Plugins.Loader --version 0.23.0
NuGet\Install-Package NetworkInspector.Plugins.Loader -Version 0.23.0
<PackageReference Include="NetworkInspector.Plugins.Loader" Version="0.23.0" />
<PackageVersion Include="NetworkInspector.Plugins.Loader" Version="0.23.0" />
<PackageReference Include="NetworkInspector.Plugins.Loader" />
paket add NetworkInspector.Plugins.Loader --version 0.23.0
#r "nuget: NetworkInspector.Plugins.Loader, 0.23.0"
#:package NetworkInspector.Plugins.Loader@0.23.0
#addin nuget:?package=NetworkInspector.Plugins.Loader&version=0.23.0
#tool nuget:?package=NetworkInspector.Plugins.Loader&version=0.23.0
NetworkInspector.Plugins.Loader
The generic loader for the plugin kinds of any application: it reads each plugin's identity, version, host requirement,
kinds, and licenses from the file without running it, keeps one plugin per name, loads each plugin assembly in its own
load context, configures each plugin once per plugin kind the application defines, commits all or nothing per plugin,
and starts and stops the plugins with time limits. Every call into plugin code is bounded and contained. The ni command-line tool loads its plugins of the
kind networkinspector with this package; any application defines its own kinds with
NetworkInspector.Plugins.Abstractions. The loader references
neither Network Inspector's contract nor Core.
You do not need this package to write a plugin; a plugin references only the contract of its kind (for Network
Inspector NetworkInspector.Plugins).
Install
dotnet add package NetworkInspector.Plugins.Loader
dotnet add package NetworkInspector.Plugins.Registry # Network Inspector's kind and registry
dotnet add package NetworkInspector.BuiltInPlugins # the standard protocols, sources, and formats as plugins
Load, run, and stop plugins
using NetworkInspector.BuiltInPlugins;
using NetworkInspector.Core;
using NetworkInspector.Core.Settings;
using NetworkInspector.Limits;
using NetworkInspector.Limits.Utility;
using NetworkInspector.Plugins.Abstractions;
using NetworkInspector.Plugins.Loader;
using NetworkInspector.Plugins.Registry;
using NetworkInspector.Settings;
string appFolder = Path.Combine(Path.GetTempPath(), "my-analyzer");
string pluginFolder = args[0];
// 1. Application settings: the plugin paths and the five plugin limits.
using SettingsManager application = new();
PluginPathsSetting.Register(application);
PluginLimits.Register(application);
Limit Live(LimitDefinition definition) => application.GetLiveLimit(definition.Key)!;
PluginLimitTexts texts = PluginLimitTexts.WithChangeHint(static definition => $"Change {definition.Key} in the settings.");
// 2. Scan by metadata (no plugin code runs), then load the kind networkinspector into a registry.
using PluginRegistry registry = new();
PluginLoader loader = new(new PluginLoaderOptions(Path.Combine(appFolder, "plugin-scan-cache.json"), "My Analyzer",
new SemanticVersion(1, 0, 0), [NetworkInspectorPlugins.Kind(registry)], NetworkInspectorBuiltIns.Types,
new PluginLoaderLimits(Live(PluginLimits.MetadataSize), Live(PluginLimits.CallTimeout)), texts));
PluginScan scan = loader.Scan([pluginFolder], CancellationToken.None);
PluginLoadReport report = await loader.LoadAsync(scan, CancellationToken.None);
foreach (PluginRecord record in report.Plugins)
{
Console.WriteLine($"{record.Identity.Name} {record.Identity.Version}: {record.State} {record.Reason}");
}
// 3. Plugin settings, then a stack with every protocol of every loaded plugin.
registry.ApplySettings(SettingScope.Application, application);
using SettingsManager profile = new();
registry.ApplySettings(SettingScope.Profile, profile);
StackBuilder builder = new(new SettingsManager(), new FrameInterfaceRegistry());
registry.ApplyStackContributions(builder);
using Stack stack = builder.Build();
// 4. Start the plugins (each bounded by plugins.startTimeout); each gets a context with its log, data folder, and settings.
Dictionary<string, PluginContext> contexts = new(StringComparer.OrdinalIgnoreCase);
IPluginContext ContextFor(PluginRecord record)
{
string name = record.Identity.Name;
if (!contexts.TryGetValue(name, out PluginContext? context))
{
string data = Path.Combine(appFolder, "plugins-data", StorageNames.ToStorageName(name));
context = new PluginContext(name, new ConsoleLog(name), data, application, profile);
contexts[name] = context;
}
return context;
}
report = await PluginLifecycle.StartAsync(report, registry.Disable, ContextFor,
PluginLimits.TimeoutOf(Live(PluginLimits.StartTimeout)), texts, CancellationToken.None);
// 5. Stop in reverse order (each bounded by plugins.stopTimeout), then release the contexts.
await PluginLifecycle.StopAsync(report, ContextFor, PluginLimits.TimeoutOf(Live(PluginLimits.StopTimeout)), texts);
foreach (PluginContext context in contexts.Values)
{
context.Dispose();
}
sealed class ConsoleLog(string plugin) : IPluginLog
{
public void Info(string text) => Console.WriteLine($"Info {plugin}: {text}");
public void Warning(string text) => Console.WriteLine($"Warning {plugin}: {text}");
public void Error(string text, Exception? exception = null) => Console.WriteLine($"Error {plugin}: {text} {exception?.Message}");
}
With the sample plugin in the folder the program prints:
NetworkInspector.StandardProtocols 0.22.0: Loaded
NetworkInspector.FileSources 0.22.0: Loaded
NetworkInspector.RandomSource 0.22.0: Loaded
NetworkInspector.Exporters 0.22.0: Loaded
Sample.Protocol 0.1.0: Loaded
Info Sample.Protocol: started; greeting=Hello from Sample.Protocol
Info Sample.Protocol: stopped
Guard calls into plugins
After the start an application calls delegates that plugins supplied (command handlers, status readers, factories). One
PluginCalls and one PluginGuard per application contain those calls: an exception becomes one report per plugin and
contributor, a call that does not end within plugins.callTimeout is abandoned, and a plugin whose abandoned calls reach
plugins.abandonedCalls (default 8) is refused further calls and raises PluginFaulted once.
PluginCalls calls = new(Live(PluginLimits.AbandonedCalls), texts);
calls.PluginFaulted += (_, fault) => registry.Disable(fault.PluginName, fault.Reason);
PluginGuard guard = new(calls, Live(PluginLimits.CallTimeout), texts);
// Cheap callbacks on the caller's thread; everything that may block on a pool thread with the time limit.
guard.TryCall("Acme.Counter", "status item", () => statusItem.Refresh(), Console.Error.WriteLine);
PluginCallResult result = await guard.RunAsync("Acme.Counter", "command acme.reset", () => command.RunAsync(), Console.Error.WriteLine);
This is containment, not a sandbox: an unhandled exception on a thread a plugin created, an async void method that
throws, a stack overflow, and an out-of-memory condition still end the process.
What the package contains
| Type | Purpose |
|---|---|
PluginLoader, PluginLoaderOptions, PluginLoaderLimits |
Scan by metadata (one plugin per name, host check), LoadAsync (construct, configure per kind, commit) |
PluginKind, PluginKind<TPlugin, TBuilder> |
A kind the application defines: its plugin interface and its builder factory |
PluginScan, ScannedPlugin, PluginIdentity, PluginState, PrescanSummary |
The scan: per row identity, path, state (Waiting, Skipped, Failed), reason, licenses; AdmittedLicenses() |
PluginLoadReport, PluginRecord, ContributionKind, ContributionEntry, ContributionRecorder |
The load: per plugin state, reason, time, contributions in record order, licenses, instance |
PluginLifecycle, PluginContext |
StartAsync in load order and StopAsync in reverse order, each bounded; the context a started plugin receives |
PluginCalls, PluginCallResult, PluginFaultedEventArgs, PluginGuard |
Bounded and contained calls into plugin code after the start |
PluginLimits, PluginLimitTexts |
plugins.metadataSize, callTimeout, startTimeout, stopTimeout, abandonedCalls and their hit sentences |
PluginPathsSetting, PluginFailures |
The setting plugins.paths; the one-line text of a failure in plugin code |
Guarantees
- No plugin code before
LoadAsync. The scan reads metadata withSystem.Reflection.Metadata; every identity value, version text, and license inventory is bounded byplugins.metadataSize. Results are cached per file (path, length, last write time). - Folders are searched directly. A folder in the plugin paths contributes the
.dllfiles directly inside it, not those in its subfolders; list each plugin folder as its own entry. - One plugin per name. Names compare ignoring case. A built-in beats a file; among files the highest semantic version
wins, equal versions keep the first in load order; a plugin whose
[PluginRequiresHost]excludes the host version is skipped before the name is decided. Every loser names its winner. - Kinds by type, not by name. A plugin is configured once for each defined kind whose interface it implements by type identity; a plugin of an undefined kind is skipped without loading.
- Bounded calls. Construction and every configure call run on a pool thread bounded by
plugins.callTimeout;StartAsyncandStopAsyncare bounded byplugins.startTimeoutandplugins.stopTimeout. - All or nothing per plugin. Each kind's builder stages; the plugin's builders are committed together after every
plugin was configured, or all discarded. A plugin that calls
CommitorDiscardon its own builder fails. - One identity for shared types. An assembly that the application already has always comes from the application, matched by name; other dependencies resolve from the plugin folder, so two plugins can ship different versions of a private library. A plugin built against a newer application assembly fails before any of its code runs, naming both versions.
- Plugins stay loaded. A loaded plugin cannot be unloaded while the process runs; a file without a loaded plugin is released at once.
Thread safety
PluginLoader runs one Scan or LoadAsync at a time per instance. PluginCalls and PluginGuard are thread-safe; one
of each serves the whole application. PluginLifecycle calls the plugins sequentially. PluginContext members are
thread-safe. Network Inspector's contribution registry is PluginRegistry of
NetworkInspector.Plugins.Registry.
Security
A plugin is trusted in-process code with the rights of the application. plugins.paths is empty by default, so
nothing foreign is loaded until a user adds a folder; relative entries are refused because they would mean a different
folder for every working directory. Load plugins only from folders that only trusted users can write.
More: plugin guide · contract reference · built-in plugins.
| 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. |
-
net10.0
- InvariantStringHelper (>= 1.0.0)
- NetworkInspector.Licensing (>= 0.23.0)
- NetworkInspector.Plugins.Abstractions (>= 0.23.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on NetworkInspector.Plugins.Loader:
| Package | Downloads |
|---|---|
|
NetworkInspector.BaseApp.Core
The UI-free core of a base application: identity, typed commands with one pipeline, a license gate, generic plugins, settings and profiles, messages and a log file, jobs, texts in several languages, shared UI state, and row sources for huge growing lists. It references no web or desktop framework. |
|
|
NetworkInspector.Plugins.Registry
Network Inspector's plugin contribution registry: staging builder, stack and settings contributions, sources, exporters, and export target rules. |
GitHub repositories
This package is not used by any popular GitHub repositories.