ServiceCollectionHostedServiceExtensions.AddHostedService 方法

定義

多載

AddHostedService<THostedService>(IServiceCollection)

為指定的類型新增 IHostedService 註冊。

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

為指定的類型新增 IHostedService 註冊。

AddHostedService<THostedService>(IServiceCollection)

來源:
ServiceCollectionHostedServiceExtensions.cs
來源:
ServiceCollectionHostedServiceExtensions.cs
來源:
ServiceCollectionHostedServiceExtensions.cs
來源:
ServiceCollectionHostedServiceExtensions.cs

為指定的類型新增 IHostedService 註冊。

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

類型參數

THostedService

IHostedService要註冊的 。

參數

services
IServiceCollection

要用來註冊的 IServiceCollection

傳回

原始的 IServiceCollection

備註

注意

您不應該針對相同的類別多次呼叫這個方法。 這麼做只會註冊您提供的第一個 HostedService。 若要新增多個 HostedServices,請針對 每個明確實IHostedService作使用 AddSingleton<TService>(IServiceCollection)

適用於

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

來源:
ServiceCollectionHostedServiceExtensions.cs
來源:
ServiceCollectionHostedServiceExtensions.cs
來源:
ServiceCollectionHostedServiceExtensions.cs
來源:
ServiceCollectionHostedServiceExtensions.cs

為指定的類型新增 IHostedService 註冊。

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

類型參數

THostedService

IHostedService要註冊的 。

參數

services
IServiceCollection

要用來註冊的 IServiceCollection

implementationFactory
Func<IServiceProvider,THostedService>

建立服務實作之新執行個體的 Factory。

傳回

原始的 IServiceCollection

備註

注意

您不應該針對相同的類別多次呼叫這個方法。 這麼做只會註冊您提供的第一個 HostedService。 若要新增多個 HostedServices,請針對 每個明確實IHostedService作使用 AddSingleton<TService>(IServiceCollection, Func<IServiceProvider,TService>)

適用於