2

I have this piece of code working in asp.net core application, now I want to use eventBus in genericHost based application. So, what is the equivalent code?

public static void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ConfigureEventBus(app);
}

public static void ConfigureEventBus(IApplicationBuilder app)
{
    if (app != null)
    {
        var eventBus = app.ApplicationServices.GetRequiredService<IConsumeEventBus>();

        eventBus.Subscribe<DemoEvent, DemoEventHandler>("18Julyexchange", "18Julyqueue", "18JulyRoute");
    }
}

My generic host based application code

var host = new HostBuilder()
    .ConfigureHostConfiguration(configHost =>
    {
        configHost.SetBasePath(GetContentRootPath());
        configHost.AddJsonFile("AppSettings.json", optional: true);
        configHost.AddCommandLine(args);
    })
    .ConfigureServices((hostContext, services) =>
    {


    })
    .ConfigureLogging((hostContext, configLogging) =>
    {

    })
    .UseConsoleLifetime()
    .Build();


Console.WriteLine("Hello World!");
host.RunConsoleAsync();
Nkosi
  • 235,767
  • 35
  • 427
  • 472
TrinTrin
  • 430
  • 5
  • 12

2 Answers2

2

An alternative approach

Create IHostedService derived implementation

public class EventBusHostedService : IHostedService {
    private readonly IConsumeEventBus eventBus;

    public EventBusHostedService(IConsumeEventBus eventBus) {
        this.eventBus = eventBus;
    }

    public Task StartAsync(CancellationToken cancellationToken) {
        eventBus.Subscribe<DemoEvent, DemoEventHandler>("18Julyexchange", "18Julyqueue", "18JulyRoute");

        return Task.CompletedTask;
    }


    public Task StopAsync(CancellationToken cancellationToken) {
        return Task.CompletedTask;
    }
}

Ensure what ever dependencies and the hosted service are added to the service collection

public static async Task Main(string[] args) {
    var host = new HostBuilder()
        .ConfigureHostConfiguration(configHost => {
            configHost.SetBasePath(GetContentRootPath());
            configHost.AddJsonFile("AppSettings.json", optional: true);
            configHost.AddCommandLine(args);
        })
        .ConfigureServices((hostContext, services) => {
            services.AddTransient<IConsumeEventBus>(....);
            services.AddHostedService<EventBusHostedService>();
        })
        .ConfigureLogging((hostContext, configLogging) => {

        })
        .UseConsoleLifetime()
        .Build();

    Console.WriteLine("Hello World!");
    await host.RunConsoleAsync();
}

The hosted service will be started as part of running the host and the desired functionality will be invoked.

Reference ASP.NET Core Generic Host

Nkosi
  • 235,767
  • 35
  • 427
  • 472
-4

Take a look at: https://learn.microsoft.com/de-de/aspnet/core/fundamentals/startup?view=aspnetcore-5.0

With the IWebHostBuilder.Configure you should be able to set the IApplicationBuilder

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.ConfigureServices(services =>
            {
                services.AddControllersWithViews();
            })
            .Configure(app =>
            {
                var loggerFactory = app.ApplicationServices
                    .GetRequiredService<ILoggerFactory>();
                var logger = loggerFactory.CreateLogger<Program>();
                var env = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
                var config = app.ApplicationServices.GetRequiredService<IConfiguration>();

                logger.LogInformation("Logged in Configure");

                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    app.UseHsts();
                }

                var configValue = config["MyConfigKey"];
            });
        });
    });
howardButcher
  • 331
  • 1
  • 9
  • 5
    This doesn't answer the question, which explicitly states that they want a solution using the generic host, not a web host. – Jeremy Lakeman Jul 08 '21 at 06:16