Skip to content

How to configure service with new net8 Host.CreateApplicationBuilder and bind appsettings.json which allows AOT? #103957

Closed Answered by davidfowl
cirrusone asked this question in Q&A
Discussion options

You must be logged in to vote

It's because of this:

builder.Services.AddOptions<DatabaseConfig>().BindConfiguration("DatabaseConfig"); // <- Empty
builder.Services.AddSingleton<DatabaseConfig>();

That first line is setting up IOptions<DatabaseConfig> which is where configuration is being bound, and you're also adding DatabaseConfig directly. Use IOptions<DatabaseConfig> instead.

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace WorkerServiceTest;

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger, IOptions<Database…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by cirrusone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants