1

I'm playing around in .NET Core 6 Web API's. But I can't seem to figure out how the connection string works now.

The first part here that is commented out works fine. But I need to be able to throw the program on different systems and change the connection string with appsettings.json.

The second part is what I attempted but that doesn't work.

Config connection string in .net core 6 is where I got it from.

//builder.Services.AddDbContext<TodoContext>(opt =>
//    opt.UseSqlServer(@"Data Source=JOHANDRE\\SQL2017; Database=ToDoItems; User=xxx; Password=xxx;"));

builder.Services.AddDbContext<TodoContext>(opt =>
    opt.UseSqlServer(builder.Configuration.GetConnectionString("ToDoItemsDatabase")));

My appsettings.json:

"ConnectionStrings": {
      "ToDoItemsDatabase": "Server=JOHANDRE\\SQL2017; Database=ToDoItems; User=xxx; Password=xxx;"
  },

I want to add that it does not throw errors. it just does not seem to find the connection.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Johandre
  • 55
  • 2
  • 10
  • 1
    You do realize the connection strings are different right? `Data Source=JOHANDRE\\SQL2017;` vs `Server=JOHANDRE\\SQL2017; ` – sommmen Jan 08 '22 at 12:05
  • Yes, I was trying both ways, neither Server nor Data Source works in the appsettings, but i can use either directly in Program.cs.. Must have just missed changing it back to the same for the question – Johandre Jan 08 '22 at 12:15
  • okay any error/log messages? like this can be anything – sommmen Jan 08 '22 at 13:15
  • Most probably, the builder's configuration is not the one you expect. Can you show us what the builder is? – Métoule Jan 08 '22 at 16:34
  • I wonder if it might not be related to the connectionString though as I run it as a background service and noticed other strings stored in the appsettings such as my ApiKey also does not work. so im thinking it might be related to how background tasks work. I will do some more research and if it was a error related here I will post the answer or findings otherwise I will just close the question but thank you so far for trying to help. – Johandre Jan 10 '22 at 17:06
  • @Métoule Im not sure what you are referring to here or where to look/find it – Johandre Jan 10 '22 at 17:07
  • Can you please provide full repro code? And how do you diagnose: _"it just does not seem to find the connection."_? – Guru Stron Jan 10 '22 at 17:08
  • 1
    @GuruStron When you run a service you can attach a debugger to it. Doing this I can see the service runs fine but the connString gets set to 'null' I tested it on another project also and once it runs as a service my dbCon and ApiKey got set to null. Hope im sharing this correctly. https://github.com/Johandre296/WinServiceTest.git is this WinService repo in admin cmd you can just say "sc create "serviceName" binPath="where ever you put\theExe" Then just start it under services. https://github.com/Johandre296/ToDoItems.git the test project. – Johandre Jan 10 '22 at 17:32
  • @Jorra AFAIK null connection string should result in exception. – Guru Stron Jan 10 '22 at 17:34
  • @GuruStron it does, but because it runs as a service and not a solution it kinda seems to step over it and tries to continue (im not sure how or why im still new to this whole background service thing). But in windows event viewer it picks up the exception of a null conString as well. It also starts up and sets the conString but no initial request or action is taking so that may be why it doesnt just out right break and die and just kinda hangs in limbo – Johandre Jan 10 '22 at 17:41
  • @GuruStron just found this https://stackoverflow.com/a/24728247/5474819 I believe this might be my issue. Will have a look, otherwise i will be back. For now i am going to close this now to avoid adding more spam questions that are already answered. but thank you for trying – Johandre Jan 10 '22 at 17:43

1 Answers1

2

Problem is how you start your Web API from the service. You are using Process without setting ProcessStartInfo.WorkingDirectory to the folder containing exe and configuration and the started process shares the working directory with parent one, so either move appsettings.json to the parent project folder or set the WorkingDirectory to match the directory containing the exe:

toDoTest.StartInfo.UseShellExecute = false;
toDoTest.StartInfo.WorkingDirectory = "C:\\Develop\\ToDoMVCtutorial\\bin\\Release\\net6.0\\publish\\";

Also you can try redirecting your Web API output to capture the logs.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132