Skip to content

Latest commit

 

History

History

Bet.Extensions.Hosting

Bet.Extensions.Hosting

GitHub license Build status NuGet feedz.io

The second letter in the Hebrew alphabet is the ב bet/beit. Its meaning is "house". In the ancient pictographic Hebrew it was a symbol resembling a tent on a landscape.

Note: Pre-release packages are distributed via feedz.io.

Summary

The collection of the IHost related functionality used with GenericHost.

buymeacoffee

Give a Star! ⭐

If you like or are using this project to learn or start your solution, please give it a star. Thanks!

Install

    dotnet add package Bet.Extensions.Hosting
    dotnet add package Bet.Extensions.Options

Usage

Configuration Validations for IHost

Usage Bet.Extensions.Options.

To enable validation use UseOptionValidation in Program.cs

        var host = new HostBuilder()
                .UseOptionValidation().Build();

Usage of ConfigureWithDataAnnotationsValidation or ConfigureWithValidation the same as in IWebHost

IHostStartupFilter

To enable registration of other services on the start up use UseStartupFilters in Program.cs

By implementing and registering this interface with DI it is possible to trigger startup jobs for IHost.

ITimedHostedService

The simple implementation of the service that must be run at an interval specified.

  1. Add MyHostedService to the DI.
    services.AddTimedHostedService(
        "MachineLearningService",
        options =>
        {
            options.Interval = TimeSpan.FromMinutes(30);

            options.FailMode = FailMode.LogAndRetry;
            options.RetryInterval = TimeSpan.FromSeconds(30);

            options.TaskToExecuteAsync = async (sp, cancellationToken) =>
            {
                var job = sp.GetRequiredService<IModelCreationService>();
                var logger = sp.GetRequiredService<ILogger<TimedHostedService>>();

                try
                {
                    await job.BuildModelsAsync(cancellationToken);
                }
                catch (Exception ex)
                {
                    logger.LogError("{serviceName} failed with exception: {message}", nameof(TimedHostedService), ex.Message);
                }
            };
        });