Show / Hide Table of Contents

Class HostedServiceDefault<TStartup>

Abstract default class that must be inherit to register a HostedService.

Inheritance
System.Object
HostedServiceDefault<TStartup>
Namespace: StoneCo.Framework.HostedService
Assembly: cs.temp.dll.dll
Syntax
public abstract class HostedServiceDefault<TStartup> : IHostedService where TStartup : class
Type Parameters
Name Description
TStartup

Concrete class that inherits this.

Constructors

HostedServiceDefault(IConfiguration, IServiceProvider)

Default constructor.

Declaration
protected HostedServiceDefault(IConfiguration configuration, IServiceProvider serviceProvider)
Parameters
Type Name Description
IConfiguration configuration

Represents a set of key/value application configuration properties.

System.IServiceProvider serviceProvider

Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.

Examples

First of all, make a class inheriting HostedServiceDefault class:

public class FooHostService : HostedServiceDefault<FooHostService>, IHostedService
{
    IBarService BarService { get; }

    public EventProcessorHostService(IBarService barService, IServiceProvider serviceProvider, IConfiguration configuration) :
        base(serviceProvider, configuration)
    {
        BarService = barService;
    }

    public async override Task StartAction(CancellationToken cancellationToken)
    {
        if (BarService != null) await BarService.RegisterConsumerTestAsync("queue_test");

        cancellationToken.Register(() => StopAsync(cancellationToken));
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        if (BarService != null) await BarService.UnRegisterAllConsumerTestAsync();
    }
}

Now, register injection dependency on DI Factory:

services.AddHostApiLog(configuration);
services.AddHostedService<FooHostService>();
services.AddHttpContextAccessor();

Properties

Configuration

Represents a set of key/value application configuration properties.

Declaration
protected IConfiguration Configuration { get; }
Property Value
Type Description
IConfiguration

Injected object by DI.

ServiceProvider

Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.

Declaration
protected IServiceProvider ServiceProvider { get; }
Property Value
Type Description
System.IServiceProvider

Injected object by DI.

TaceKey

Key of trace.

Declaration
public string TaceKey { get; set; }
Property Value
Type Description
System.String

Sample a4477c7f88264bc1973e55856c8cfbd2

Methods

StartAction(CancellationToken)

Abstract method that will be triggered by StartAsync method, using try/catch block to register an error occasionally.

Declaration
public abstract Task StartAction(CancellationToken cancellationToken)
Parameters
Type Name Description
CancellationToken cancellationToken

Indicates that the start process has been aborted.

Returns
Type Description
Task

Task

StartAsync(CancellationToken)

Virtual method implementation that will be triggered when the application host is ready to start the service.

Declaration
public virtual Task StartAsync(CancellationToken cancellationToken)
Parameters
Type Name Description
CancellationToken cancellationToken

Indicates that the start process has been aborted.

Returns
Type Description
Task

Task

Stop(CancellationToken)

Abstract method that will be triggered by StopAsync method, using try/catch block to register an error occasionally.

Declaration
public abstract Task Stop(CancellationToken cancellationToken)
Parameters
Type Name Description
CancellationToken cancellationToken

Indicates that the shutdown process should no longer be graceful.

Returns
Type Description
Task

Task

StopAsync(CancellationToken)

Virtual method implementation that will be triggered when the application host is performing a graceful shutdown.

Declaration
public virtual Task StopAsync(CancellationToken cancellationToken)
Parameters
Type Name Description
CancellationToken cancellationToken

Indicates that the shutdown process should no longer be graceful.

Returns
Type Description
Task

Task

Back to top Generated by DocFX