static void Run(CmdOptions options) { var host = new HostBuilder() .ConfigureAppConfiguration((hostContext, config) => { config.AddJsonFile("appsettings.json", optional: true); }) .ConfigureLogging((context, factory) => { factory.AddConsole(); }) .ConfigureServices((context, services) => { services.AddSingleton <CmdOptions>(options); var kafkaMessageBusOptions = context.Configuration.GetSection("kafka").Get <KafkaMessageBusOptions>(); kafkaMessageBusOptions.ClientMode = options.Mode; //这里方便测试,以命令行参数为准 services.AddKafkaMessageBus(kafkaMessageBusOptions); // AddKafkaMessageBus(services); if ((options.Mode & ClientMode.Consumer) > 0) { services.AddHostedService <MessageBusConsumeService>(); } if ((options.Mode & ClientMode.Producer) > 0) { services.AddHostedService <MessageBusProduerService>(); } }); host.RunConsoleAsync().Wait(); Console.WriteLine("服务已退出"); }
public MessageBusProduerService(ILogger <MessageBusProduerService> logger, IMessageBus messageBus, CmdOptions cmdOptions) { _logger = logger; _messageBus = messageBus; _cmdOptions = cmdOptions; }