Atc.Hosting
1.0.15
See the version list below for details.
dotnet add package Atc.Hosting --version 1.0.15
NuGet\Install-Package Atc.Hosting -Version 1.0.15
<PackageReference Include="Atc.Hosting" Version="1.0.15" />
paket add Atc.Hosting --version 1.0.15
#r "nuget: Atc.Hosting, 1.0.15"
// Install Atc.Hosting as a Cake Addin #addin nuget:?package=Atc.Hosting&version=1.0.15 // Install Atc.Hosting as a Cake Tool #tool nuget:?package=Atc.Hosting&version=1.0.15
Atc.Hosting
Contains various classes and extensions methods for the hosting namespace, e.g. BackgroundServices
Why BackgroundServiceBase<T>
instead of BackgroundService
BackgroundServiceBase
extend the BackgroundService
with features like:
- High-performance logging by using source-generated logging using
ILogger
- Built-in retryies using
Polly
- Error handling using logging instead of service crashing silently
- Configuration for:
- Startup-Delay
- Retry-Count
- Repeat-Interval
- Simple to use
Using BackgroundServiceBase<T>
A sample reference implementation can be found in the sample project Atc.Hosting.TimeFile.Sample
which shows an example of the service TimeFileWorker
that uses BackgroundServiceBase
.
Program.cs
In Program.cs
the TimeFileWorker
is wired up by using AddHostedService<T>
as a normal BackgroundService
.
Note: TimeFileWorker
uses TimeFileWorkerOptions
that implements IBackgroundServiceOptions
.
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var host = Host
.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddSingleton<ITimeService, TimeService>();
services.Configure<TimeFileWorkerOptions>(configuration.GetSection(TimeFileWorkerOptions.SectionName));
services.AddHostedService<TimeFileWorker>();
})
.Build();
host.Run();
TimeFileWorker.cs
In this example TimeFileWorker
, it inherits from BackgroundServiceBase
. It shows an example of the only method DoWorkAsync
which has to be implemented.
public class TimeFileWorker : BackgroundServiceBase<TimeFileWorker>
{
private readonly TimeFileWorkerOptions workerOptions;
private readonly ITimeService timeService;
public TimeFileWorker(
ILogger<TimeFileWorker> logger,
IOptions<TimeFileWorkerOptions> workerOptions,
ITimeService timeService)
: base(
logger,
workerOptions.Value)
{
this.workerOptions = workerOptions.Value;
this.timeService = timeService;
}
public override Task DoWorkAsync(
CancellationToken stoppingToken)
{
Directory.CreateDirectory(workerOptions.OutputDirectory);
var time = timeService.GetDateTime();
var outFile = Path.Combine(
workerOptions.OutputDirectory,
$"{time:yyyy-MM-dd--HHmmss}.txt");
return File.WriteAllTextAsync(outFile, ServiceName, stoppingToken);
}
}
Extensions for ServiceProvider
Defined extensions methods for ServiceProvider:
GetHostedService
<T>
Using GetHostedService<T>
Example on how to retrieve the BackgroundService from the HttpContext in a MVC controller or MinimalApi endpoint.
In this example we are working with the TimeFileWorker
BackgroundService.
Note: Remember to wire up BackgroundService in Program.cs
by adding this line services.AddHostedService<TimeFileWorker>();
.
Example setup for a MVC controller:
[HttpGet("my-method")]
public void GetMyMethod()
{
var timeFileWorker = httpContext.RequestServices.GetHostedService<TimeFileWorker>();
if (timeFileWorker is not null)
{
// Here we have access to the TimeFileWorker instance.
}
}
Example setup for a MinimalApi endpoint:
public void DefineEndpoints(WebApplication app)
{
app.MapGet("my-method", async httpContext =>
{
var timeFileWorker = httpContext.RequestServices.GetHostedService<TimeFileWorker>();
if (timeFileWorker is not null)
{
// Here we have access to the TimeFileWorker instance.
}
await Task.CompletedTask;
});
}
Requirements
How to contribute
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net7.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
- Polly (>= 7.2.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.