I am trying out new application configuration method using WebApplication.CreateBuilder(args)
method, I am trying to run web application and hosted service in the same application. Running just web application works fine but when I add
builder.Services.AddHostedService<Worker>();
line to add a hosted service, web application doesn’t work anymore and only hosted service seem to be working.
Is there a way to achieve running both applications at the same time with the same Dependency injection container?
I found this answer here, but it uses Host.CreateDefaultBuilder(args)
, and I want to achieve this by using WebApplication.CreateBuilder(args);
and more linear configuration instead of callbacks.
for reference here’s my code in Program.cs
// imports ...
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddOptions<AppConfigurationModel>().BindConfiguration("AppSettings");
builder.Services.AddDbContext<SportsbookPlatformContext>((services, opts) =>
{
...
});
builder.Services.AddEnyimMemcached();
builder.Services.AddWindowsService();
// builder.Services.AddHostedService<Worker>(); // by adding this line web application stops working
var app = builder.Build();
app.UseRouting();
app.MapControllers();
app.Run();