Del via


ServiceCollectionHostedServiceExtensions.AddHostedService Method

Definition

Overloads

AddHostedService<THostedService>(IServiceCollection)

Add an IHostedService registration for the given type.

AddHostedService<THostedService>(IServiceCollection, Func<IServiceProvider,THostedService>)

Add an IHostedService registration for the given type.

AddHostedService<THostedService>(IServiceCollection)

Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs

Add an IHostedService registration for the given type.

public:
generic <typename THostedService>
 where THostedService : class, Microsoft::Extensions::Hosting::IHostedService[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::DependencyInjection::IServiceCollection ^ AddHostedService(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ services);
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHostedService<THostedService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where THostedService : class, Microsoft.Extensions.Hosting.IHostedService;
static member AddHostedService : Microsoft.Extensions.DependencyInjection.IServiceCollection -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'HostedService : null and 'HostedService :> Microsoft.Extensions.Hosting.IHostedService)
<Extension()>
Public Function AddHostedService(Of THostedService As {Class, IHostedService}) (services As IServiceCollection) As IServiceCollection

Type Parameters

THostedService

An IHostedService to register.

Parameters

services
IServiceCollection

The IServiceCollection to register with.

Returns

The original IServiceCollection.

Remarks

Note

You shouldn't call this method multiple times for the same class. Doing so will only register the first HostedService you provide. To add multiple HostedServices, use AddSingleton<TService>(IServiceCollection) for each implementation of IHostedService explicitly.

Applies to

AddHostedService<THostedService>(IServiceCollection, Func<IServiceProvider,THostedService>)

Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs

Add an IHostedService registration for the given type.

public:
generic <typename THostedService>
 where THostedService : class, Microsoft::Extensions::Hosting::IHostedService[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::DependencyInjection::IServiceCollection ^ AddHostedService(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ services, Func<IServiceProvider ^, THostedService> ^ implementationFactory);
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHostedService<THostedService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Func<IServiceProvider,THostedService> implementationFactory) where THostedService : class, Microsoft.Extensions.Hosting.IHostedService;
static member AddHostedService : Microsoft.Extensions.DependencyInjection.IServiceCollection * Func<IServiceProvider, 'HostedService (requires 'HostedService : null and 'HostedService :> Microsoft.Extensions.Hosting.IHostedService)> -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'HostedService : null and 'HostedService :> Microsoft.Extensions.Hosting.IHostedService)
<Extension()>
Public Function AddHostedService(Of THostedService As {Class, IHostedService}) (services As IServiceCollection, implementationFactory As Func(Of IServiceProvider, THostedService)) As IServiceCollection

Type Parameters

THostedService

An IHostedService to register.

Parameters

services
IServiceCollection

The IServiceCollection to register with.

implementationFactory
Func<IServiceProvider,THostedService>

A factory to create new instances of the service implementation.

Returns

The original IServiceCollection.

Remarks

Note

You shouldn't call this method multiple times for the same class. Doing so will only register the first HostedService you provide. To add multiple HostedServices, use AddSingleton<TService>(IServiceCollection, Func<IServiceProvider,TService>) for each implementation of IHostedService explicitly.

Applies to