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?

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • https://stackoverflow.com/questions/65110479/how-to-get-values-from-appsettings-json-in-a-console-application-using-net-core – Roman Ryzhiy Nov 30 '22 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. – gunr2171 Nov 30 '22 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. – MortenMoulder Nov 30 '22 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. – MortenMoulder Nov 30 '22 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. – Douglas Ferreira Nov 30 '22 at 14:55
  • @MortenMoulder Stackoverflow is for answers, not questions. Have you read the second answer in the linked page? – Roman Ryzhiy Nov 30 '22 at 15:00
  • @RomanRyzhiy Hi Roman. I have been on StackOverflow for 9 years and 9 months. I have 257 questions and 123 answers, and I have reached over 750.000 people in total. Rooky numbers compared to others, but it's still something. I'm not trying to sound like a smartass, but your comment does not link to a duplicate question, where I can find an answer to my question. If you read my question and the answers on the link you sent, you will see it does not solve my problem. – MortenMoulder Nov 30 '22 at 15:03
  • @MortenMoulder Are you shure that a possibility to open an `appsettings.json` from anywhere you want, create a new `ConfigurationBuilder` based on that and do with it whatever you want doesn't solve your problem? – Roman Ryzhiy Nov 30 '22 at 15:09
  • @RomanRyzhiy How does it solve my problem, when the Worker project doesn't have a reference to the API project (and it shouldn't), and the appsettings.json file is in the API project? – MortenMoulder Nov 30 '22 at 15:10
  • @MortenMoulder are they in different galaxies? – Roman Ryzhiy Nov 30 '22 at 15:11
  • @MortenMoulder is the `appsettings.json` accessible via the filesystem? – Roman Ryzhiy Nov 30 '22 at 15:12
  • @RomanRyzhiy Yes, but I am not referencing the appsettings.json file with an absolute or relative path from another project. The two projects aren't even going to be deployed on the same system, so if that was your suggestion, it wouldn't work. – MortenMoulder Nov 30 '22 at 15:13
  • @MortenMoulder So the task is to have a single configuration source that is accessible by both systems? – Roman Ryzhiy Nov 30 '22 at 15:16
  • @RomanRyzhiy Yes exactly. I'm not sure what the best practise is in that regard. – MortenMoulder Nov 30 '22 at 15:22
  • @MortenMoulder One of possible ways is an External Configuration Store https://learn.microsoft.com/en-us/previous-versions/msp-n-p/dn589803(v=pandp.10)?redirectedfrom=MSDN – Roman Ryzhiy Nov 30 '22 at 15:27

0 Answers0