0

I've created three new .NET 7 projects:

  • API
  • Database
  • Worker

In my Database project, I have my DbContext set up. The API and Worker project both have a project reference to the Database project.

In my API project (using minimal hosting model), I simply configure the database connection like this:

var builder = WebApplication.CreateBuilder(args);
....
....
....

builder.Services.AddDbContext<EnergyPricesContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
);

However, my Worker project is a console application, so I cannot simply do the above line, because there is no builder, no dependency injection, and there is also no appsettings.json file in that project.

I thought about creating a bootstrap project, which would have the appsettings.json file as well as a "Startup" file that all projects have a reference to. However, I feel like that's a bad idea.

What's best practise when it comes to multiple projects and referencing the same appsetting.json file, if that's even the correct way to do it?

15
  • stackoverflow.com/questions/65110479/… Commented Nov 30, 2022 at 14:41
  • 1
    Fun fact, an "ASP.net (Core)" project is also a console application. Try using Visual Studio to create a new project using the "Worker Service" template and examine what you get in the Program.cs file. Commented Nov 30, 2022 at 14:41
  • @RomanRyzhiy Not the same question. I can easily create an appsettings.json file in my console app, but having two identical files (well, with two identical configurations) is a huge no-go. I already have the file in my API project. Commented Nov 30, 2022 at 14:43
  • @gunr2171 So is an API project. I can make my console app (with an executable file) do the same thing as the API project, but I still run into the issue of having two identical files. Not something I really want, because I would have to maintain two files at the same time. Commented Nov 30, 2022 at 14:45
  • I think the cleaner approach is having a 4th project assembly with a static class maybe even an extension method to obtain these configurations. Commented Nov 30, 2022 at 14:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.