Mohr.Jonas.Spaceshot
2.0.5
dotnet add package Mohr.Jonas.Spaceshot --version 2.0.5
NuGet\Install-Package Mohr.Jonas.Spaceshot -Version 2.0.5
<PackageReference Include="Mohr.Jonas.Spaceshot" Version="2.0.5" />
<PackageVersion Include="Mohr.Jonas.Spaceshot" Version="2.0.5" />
<PackageReference Include="Mohr.Jonas.Spaceshot" />
paket add Mohr.Jonas.Spaceshot --version 2.0.5
#r "nuget: Mohr.Jonas.Spaceshot, 2.0.5"
#:package Mohr.Jonas.Spaceshot@2.0.5
#addin nuget:?package=Mohr.Jonas.Spaceshot&version=2.0.5
#tool nuget:?package=Mohr.Jonas.Spaceshot&version=2.0.5
🚀 Spaceshot - An alternative apphost
About
Spaceshot in an apphost, similar to packages like Microsoft.Extensions.Hosting
Features
- Dependency injection for services
- Dynamic service creating / starting / stopping at runtime
- Minimal, easily maintainable codebase
Dependency injection
Dependency injection is configured via Spaceshot.ConfigurePayload().
Injection strategies
Dependency injection support three strategies:
Singleton: The value will be created once during the lifetime of the applicationScoped: The value is created once required and will persist until disposed at which point it will be recreated the next time it is requiredOneShotThe value is created every time it is required
Registering values
Values can be injected at startup via Spaceshot.ConfigurePayload() or during runtime via the magic PayloadConfigurator object.
configurator.RegisterSingleton(static (_, _) => LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(LogLevel.Trace);
builder.AddSimpleConsole(conf =>
{
conf.IncludeScopes = true;
conf.SingleLine = false;
});
}));
configurator.RegisterOneShot(static (type, injector) => injector.Inject<ILoggerFactory>().CreateLogger(type));
configurator.RegisterSingleton(static (_, _) => new ProgramSettings());
The injector
The injector is used to retrieve values from the dependency injection container via its methods:
public TInjectable Inject<TInjectable>() { ... }
public TInjectable InjectKeyed<TInjectable>(string key) { ... }
Magic objects
Magic object are objects that can be retrieved via dependency injection and are always automatically available.
The Spaceship object
The spaceship object is a special object that can be retrieved at runtime.
It functions as a controller for the running apphost, allowing the user to stop it.
The PayloadEvictor
The payload evictor can be used to evict values from the dependency injection container, making this tool essential for using Scoped values.
The PayloadConfigurator
The payload configurator is used to add dependency injection values at runtime.
Services
Services are configured via Spaceshot.ConfigureControls().
The available services are:
ISetup: Runs once at apphost start, used to set up dependencies, create required files, parsing arguments, ...IAsyncSetup: Same asISetup, but runs asynchronouslyITeardown: Runs once at apphost stop, used to save files, dispose objects, ...IAsyncTeardown: Same asITeardown, but runs asynchronouslyIBackgroundService: Combination ofISetupandITeardownIAsyncBackground: Same asIBackgroundService, but runs asynchronously
spaceshot.ConfigureControls(static configurator =>
{
configurator.RegisterSetup<ParseCliSetup>(static _ => new ParseCliSetup());
configurator.RegisterAsyncTeardown<DBDisposer>(static injector => new DBDisposer(
injector.Inject<ILogger>(),
injector.Inject<SqliteConnection>()
));
});
Configuring startup / teardown sequence
The startup and teardown sequence can be configured via the PriorityAttribute.
The attribute supports the following priorities, execute in the following order:
HighestHighMediumLowLowest
Assuming you want to configure the ParseCliSetup to run first, the PriorityAttribute can be used as follows:
[Priority(Priority.Highest)]
internal sealed class ParseCliSetup : ISetup
{
public void Setup() { ... }
}
Full example
internal static class Program
{
private static void Main()
{
using var factory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(conf =>
{
conf.IncludeScopes = true;
conf.SingleLine = false;
}));
var logger = factory.CreateLogger(typeof(Spaceshot));
var spaceship = new Spaceshot(logger);
spaceship.ConfigurePayload(static configurator =>
{
configurator.RegisterSingleton(static (_, _) => LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(LogLevel.Trace);
builder.AddSimpleConsole(conf =>
{
conf.IncludeScopes = true;
conf.SingleLine = false;
});
}));
configurator.RegisterOneShot(static (type, injector) => injector.Inject<ILoggerFactory>().CreateLogger(type));
configurator.RegisterSingleton(static (_, _) => new ProgramSettings());
});
spaceship.ConfigureControls(static configurator =>
{
configurator.RegisterSetup(static injector => new SigtermCatcher(
injector.Inject<ILogger>(),
injector.Inject<Spaceship>()
));
configurator.RegisterAsyncSetup(static injector => new DbMigrator(
injector.Inject<ILogger>(),
injector.Inject<PayloadConfigurator>(),
injector.Inject<ProgramSettings>()
));
configurator.RegisterAsyncTeardown(static injector => new DBDisposer(
injector.Inject<ILogger>(),
injector.Inject<SqliteConnection>()
));
});
spaceship.LiftOff();
}
}
| Product | Versions 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 was computed. 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 was computed. 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. |
-
net8.0
- Microsoft.Extensions.Logging (>= 9.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.