ServiceCollectionHostedServiceExtensions.AddHostedService Metod

Definition

Överlagringar

Name Description
AddHostedService<THostedService>(IServiceCollection)

Lägg till en IHostedService registrering för den angivna typen.

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

Lägg till en IHostedService registrering för den angivna typen.

AddHostedService<THostedService>(IServiceCollection)

Källa:
ServiceCollectionHostedServiceExtensions.cs
Källa:
ServiceCollectionHostedServiceExtensions.cs
Källa:
ServiceCollectionHostedServiceExtensions.cs
Källa:
ServiceCollectionHostedServiceExtensions.cs

Lägg till en IHostedService registrering för den angivna typen.

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

Typparametrar

THostedService

En IHostedService att registrera.

Parametrar

services
IServiceCollection

Att IServiceCollection registrera med.

Returer

Den ursprungliga IServiceCollection.

Exempel

Följande kod visar hur du registrerar en värdbaserad tjänst samtidigt som den faktiska THostedService typen registreras.

services.AddSingleton<SomeService>();
services.AddHostedService(sp => sp.GetRequiredService<SomeService>());

Kommentarer

Observera att detta skapar registrering för IHostedService specifikt. Inte för den faktiska THostedService typen. Om du vill registrera den faktiska typen måste du göra det separat.

Gäller för

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

Källa:
ServiceCollectionHostedServiceExtensions.cs
Källa:
ServiceCollectionHostedServiceExtensions.cs
Källa:
ServiceCollectionHostedServiceExtensions.cs
Källa:
ServiceCollectionHostedServiceExtensions.cs

Lägg till en IHostedService registrering för den angivna typen.

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

Typparametrar

THostedService

En IHostedService att registrera.

Parametrar

services
IServiceCollection

Att IServiceCollection registrera med.

implementationFactory
Func<IServiceProvider,THostedService>

En fabrik för att skapa nya instanser av tjänstimplementeringen.

Returer

Den ursprungliga IServiceCollection.

Exempel

Följande kod visar hur du registrerar en värdbaserad tjänst samtidigt som den faktiska THostedService typen registreras.

services.AddSingleton<SomeService>(implementationFactory);
services.AddHostedService(sp => sp.GetRequiredService<SomeService>());

Kommentarer

Observera att detta skapar registrering för IHostedService specifikt. Inte för den faktiska THostedService typen. Om du vill registrera den faktiska typen måste du göra det separat.

Gäller för