C
C#2y ago
danno8524

❔ Access AppSettings.json in asp.net core

I am receiving this error when attempting to access the AppSettings. It does not like the Configuration. What am I missing here?
20 Replies
Bladesfist
Bladesfist2y ago
Static methods can't access instance members Do you need that method do be static?
danno8524
danno8524OP2y ago
If I remove the static it errors out on my call to method... I guess I don't really understand static... What should my syntax be here?
Bladesfist
Bladesfist2y ago
Why are you trying to call methods on a controller directly?
danno8524
danno8524OP2y ago
I am calling code for reusability...
Bladesfist
Bladesfist2y ago
Is this an ASP controller or does controller mean something different in this context, if it is, you probably want to split your querying out into a service Then inject this service into the controller and the other place where you want to run this You want your controllers to be thin interfaces that describe your API endpoints and then delegate to their services to do the actual work
danno8524
danno8524OP2y ago
The way it is now is code in one controller calling code in another controller...This works... I am just trying to get my Azure keys from AppSettings instead of hard coded in controller...
Bladesfist
Bladesfist2y ago
It doesn't work, hence the error message If you really want to continue down this route, remove the static keyword from the method and inject the one controller into the other It's absolutely not good practice to do so though All your shared logic can live hapily in the service layer and these services can be reused by all your controllers / other services, it's a code smell for it to live in your controllers. If you want an example of what I mean and can post the 2 controllers, I can show you how I'd refactor it
danno8524
danno8524OP2y ago
That would be awesome! Sorry for my ignorance here. Right now I have a Home Contoller and a NowSummary Controller. This code works . In the NOWSummary controller I have have public async Task<ActiveStormKeyEntity> ExecuteActiveStormKeyTableQuery()
Bladesfist
Bladesfist2y ago
Here is some psuedo code from what you've said so far
Bladesfist
Bladesfist2y ago
BlazeBin - fiigzihkjcby
A tool for sharing your source code with the world!
Bladesfist
Bladesfist2y ago
StormKeyService would need to be registered as a scoped service in your configure services part of Program.cs or Startup.cs Both controllers can inject the service and reuse the implementation of the ExecuteActiveStormKeyTableQuery method
danno8524
danno8524OP2y ago
Thanks a bunch. Sorry for my ignorance. Just so I understand this... The Public Interface code should go where? In Visual Studio do I create a new interface and this will be a folder just like a controller?
Bladesfist
Bladesfist2y ago
I'd stick it in it's own file in the same folder as the implementation (the class that's "inheriting" from the interface)
danno8524
danno8524OP2y ago
When you say registered as a scoped service in Program.cs or startup.cs , could you elaborate on the code needed here...
Bladesfist
Bladesfist2y ago
Do you have one or both of those files? Assuming you have a new world project then you should have something like
...
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IStormKeyService, StormKeyService>();
...
...
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IStormKeyService, StormKeyService>();
...
danno8524
danno8524OP2y ago
I have a startup.cs like this...
Bladesfist
Bladesfist2y ago
Just services.AddScoped<IStormKeyService, StormKeyService>(); then in your ConfigureServices method, placement order doesn't matter
danno8524
danno8524OP2y ago
Thank you for all your help!! Much appreciated...
Bladesfist
Bladesfist2y ago
No problem, good luck with your project
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server