HostBuilder 类

定义

程序初始化实用工具。A program initialization utility.

public ref class HostBuilder : Microsoft::Extensions::Hosting::IHostBuilder
public class HostBuilder : Microsoft.Extensions.Hosting.IHostBuilder
type HostBuilder = class
    interface IHostBuilder
Public Class HostBuilder
Implements IHostBuilder
继承
HostBuilder
实现

构造函数

HostBuilder()

属性

Properties

用于在主机生成过程中共享组件间状态的中心位置。A central location for sharing state between components during the host building process.

方法

Build()

运行给定操作以初始化主机。Run the given actions to initialize the host. 这只能调用一次。This can only be called once.

ConfigureAppConfiguration(Action<HostBuilderContext,IConfigurationBuilder>)

为生成过程和应用程序的其余部分设置配置。Sets up the configuration for the remainder of the build process and application. 传入的 Configuration 是通过调用 ConfigureHostConfiguration(Action<IConfigurationBuilder>) 生成的主机配置。The Configuration passed in is the host's configuration built from calls to ConfigureHostConfiguration(Action<IConfigurationBuilder>). 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive. 处理所有调用后,将使用结果更新 Configuration 以用于将来的生成步骤。After all calls have been processed, Configuration will be updated with the results for future build steps. 生成的配置还将存储在 Services DI 容器中供使用。The resulting configuration will also be available in the Services DI Container.

ConfigureContainer<TContainerBuilder>(Action<HostBuilderContext,TContainerBuilder>)

允许配置实例化的依赖项容器。Enables configuring the instantiated dependency container. 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive.

ConfigureHostConfiguration(Action<IConfigurationBuilder>)

设置生成器自身的配置。Set up the configuration for the builder itself. 这将用于初始化 IHostEnvironment 以便稍后在生成过程中使用。This will be used to initialize the IHostEnvironment for use later in the build process. 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive.

ConfigureServices(Action<HostBuilderContext,IServiceCollection>)

向容器添加服务。Adds services to the container. 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
UseServiceProviderFactory<TContainerBuilder>(Func<HostBuilderContext,IServiceProviderFactory<TContainerBuilder>>)

重写用于创建服务提供程序的工厂。Overrides the factory used to create the service provider.

UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder>)

重写用于创建服务提供程序的工厂。Overrides the factory used to create the service provider.

扩展方法

Start(IHostBuilder)

生成并启动该主机。Builds and starts the host.

StartAsync(IHostBuilder, CancellationToken)

生成并启动该主机。Builds and starts the host.

ConfigureAppConfiguration(IHostBuilder, Action<IConfigurationBuilder>)

为生成过程和应用程序的其余部分设置配置。Sets up the configuration for the remainder of the build process and application. 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive. 可在 Configuration(用于后续操作)和 Services 中获取结果。The results will be available at Configuration for subsequent operations, as well as in Services.

ConfigureContainer<TContainerBuilder>(IHostBuilder, Action<TContainerBuilder>)

允许配置实例化的依赖项容器。Enables configuring the instantiated dependency container. 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive.

ConfigureLogging(IHostBuilder, Action<HostBuilderContext,ILoggingBuilder>)

添加一个委托来配置提供的 ILoggingBuilderAdds a delegate for configuring the provided ILoggingBuilder. 这可多次进行调用。This may be called multiple times.

ConfigureLogging(IHostBuilder, Action<ILoggingBuilder>)

添加一个委托来配置提供的 ILoggingBuilderAdds a delegate for configuring the provided ILoggingBuilder. 这可多次进行调用。This may be called multiple times.

ConfigureServices(IHostBuilder, Action<IServiceCollection>)

向容器添加服务。Adds services to the container. 可多次进行调用,并累加结果。This can be called multiple times and the results will be additive.

RunConsoleAsync(IHostBuilder, Action<ConsoleLifetimeOptions>, CancellationToken)

启用控制台支持、生成和启动主机,并等待 Ctrl+C 或 SIGTERM 关闭。Enables console support, builds and starts the host, and waits for Ctrl+C or SIGTERM to shut down.

RunConsoleAsync(IHostBuilder, CancellationToken)

启用控制台支持、生成和启动主机,并等待 Ctrl+C 或 SIGTERM 关闭。Enables console support, builds and starts the host, and waits for Ctrl+C or SIGTERM to shut down.

UseConsoleLifetime(IHostBuilder)

侦听 Ctrl+C 或 SIGTERM 并调用 StopApplication() 来启动关闭进程。Listens for Ctrl+C or SIGTERM and calls StopApplication() to start the shutdown process. 这将解除阻止 RunAsync 和 WaitForShutdownAsync 等扩展。This will unblock extensions like RunAsync and WaitForShutdownAsync.

UseConsoleLifetime(IHostBuilder, Action<ConsoleLifetimeOptions>)

侦听 Ctrl+C 或 SIGTERM 并调用 StopApplication() 来启动关闭进程。Listens for Ctrl+C or SIGTERM and calls StopApplication() to start the shutdown process. 这将解除阻止 RunAsync 和 WaitForShutdownAsync 等扩展。This will unblock extensions like RunAsync and WaitForShutdownAsync.

UseContentRoot(IHostBuilder, String)

指定主机要使用的内容根目录。Specify the content root directory to be used by the host.

UseDefaultServiceProvider(IHostBuilder, Action<ServiceProviderOptions>)

IServiceProvider 指定为默认值。Specify the IServiceProvider to be the default one.

UseDefaultServiceProvider(IHostBuilder, Action<HostBuilderContext,ServiceProviderOptions>)

IServiceProvider 指定为默认值。Specify the IServiceProvider to be the default one.

UseEnvironment(IHostBuilder, String)

指定主机要使用的环境。Specify the environment to be used by the host.

UseSystemd(IHostBuilder)

将主机生存期设置为 SystemdLifetime,为已启动和正在停止的应用程序提供通知消息,并将控制台日志记录配置为 systemd 格式。Sets the host lifetime to SystemdLifetime, provides notification messages for application started and stopping, and configures console logging to the systemd format.

UseWindowsService(IHostBuilder)

将主机生存期设置为 WindowsServiceLifetime,设置内容根,启用事件日志记录,并将应用程序名称作为默认源名称。Sets the host lifetime to WindowsServiceLifetime, sets the Content Root, and enables logging to the event log with the application name as the default source name.

适用于