❔ beginner - unable to retrieve a string from application.json
I have a solution made by 2 projects.
- in the first project I have a function in a class that retrieve a string from application.json file and I want to change this making this function calling a method of another function in the 2nd project, that retrieve this strings + make other stuffs
- well in the second project, I recreated application.json, but the same exact code that retrieve the string from application.json isn't working cause it retrieve the name of a namespace
what could it be?
I'd like to share my code but it's split in more classes and the only comfortable way is to share my screen
55 Replies
@uselessxp without code, we can't help you
$paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
thanks, now I try
it's not long but just splitted in different files and the normal paste on discord don't have colors I think so you may not understand what is what
you can use colours in markdown
that said if its too long you can just use the link above or just put the project on github or something
on discord?
btw if nothing is missed this is the code: https://paste.mod.gg/riaflzqjscza/0
BlazeBin - riaflzqjscza
A tool for sharing your source code with the world!
Actions.cs - Row 46 - here is where I get the problem
but if I paste the same row on Row 28 of Controller.cs it works
yeah
@uselessxp what is the implementation of
GlobalContext.ToString()
?what do you mean?
Can you try injecting the
GlobalContext
into DI and then getting that instead of the configuration?
You can probably get the current configuration at startup, so you can pass that on in thereI don't understand what is it cuz I'm really beginner level, but I took the same code string and pasted it into another class and it return a different value
the first project in which this works it's an API Web ASP.NET Core
the second project in which I moved the code is created as a C# Library
the application.json was automatically generated in the first project, but not in the second one, and I had to copy paste this file
maybe ti could be this that cause the problem?
You should learn to inject GlobalContext instead of this
An API has default behaviour to add appsettings to the application. That's why it was all working, and you had the file
A C# library should not provide its own appsettings because you can only have one. You need to add the file to whatever project uses the library
And usually you don't inject the whole configuration in general, but you configure a specific class to point properties to the values in the appsettings
If this confuses you, you should learn how to do this kind of thing before mindlessly copy-pasting everything, expecting it to work
yes I had to add the file manually
That aint gonna work
Your library is not going to use that
How are you using the library? Do you have an API?
I created the library, then I created the class Actions.cs and I imported it in the API project with "using"
And you referenced the library in the API?
So your API has the appsettings. No point in having it in the library
yes but his appsettings contains a connectionstring to the DB and I want to move this to the Library making impossible to read it from the API project
No? You can do that totally fine lol
my target is to remove the code in the red square that is working, replacing it with the code in the green square that call the same code present in Actions.cs
Learn how dependency injection works, and how the
Configure
extension for a ServiceCollection allows you to configure a class using your appsettings, so you can then inject this into your context and use the connection string.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-7.0Dependency injection in ASP.NET Core
Learn how ASP.NET Core implements dependency injection and how to use it.
Actions
should also be injected. This is not done.it's not enough to create an instance of it for being able to use its method .Delete() ?
You are gonna have to get the configuration which required DI either way
I stopped my projects for 2 days for follow global video tutorials on C# and what I learned is that I have to create an instance of a class for being able to access it
So it does not matter if you create it manually or let DI do it for you
No point in fixing your code if you don't know how a proper c# application is build
Dependency Injection is very common, even outside of C#
Look at what I linked. It's explained there
ok, I'll get a look to DI, but currently microsoft documentation make hard understand also the easier thing 😱
What does your program.cs file look like?
Program.cs
builder.Services
is your dependency collection, called IServiceCollection
You inject it in therethis is the only difference between API and Library so I suppose that this code implement appsettings.json somehow
If you follow the tutorial it will figure out how
No, the library can't do it. It has to have it injected from the API
Very often libraries provide methods that you use to inject everything into your application so you can use it
A library does not have a starting method. It must be called from somewhere. That's the biggest difference
damn^^ didn't expect all this.
isn't just there a way to read text from files present in the tree and?
maybe I could put the string in a .txt or tell the program to retrieve appsettings.json from its folder
yeah I noticed it, there's not Program.cs in the library, just code
Not reading this entire thread, but a solution has to import files if they're not in the same dir as the executable. If you drag and drop a file in VS, it auto imports for you
Maybe you should read the entire thread
Maybe you can, but you already have an appsettings. It does not make sense to have another
It would have to merge the json somehow and I doubt it can do that
You will then have to manually get the file and add it to your context, and this is just a big no-no
you also have to keep an eye for environment variables. visual studio uses a %appdata% folder to import env to the project, this can cause conflits if you have 2 projects in a solution
So if you just use the existing appsettings, which is done for you, you can use DI's configure method to configure a class of properties which you can then use as your configuration in GlobalContext
You effectively only have to do this once, compared to everytime you make the context manually
really? I'm making this solution for learning C# and I was suggested to make more Projects (not folders) in the same solutions, using logic, making it more professional
What he said doesn't make sense
This is not related
ok
So learn how DI works and look at how you can inject
GlobalContext
as a Scoped
(not Singleton
!!) service into your DI container, and look at how you can configure a seperate ContextConfiguration
class
The API will handle providing the GlobalContext, since it's all build in
And you can get it from the endpoint contructor and just call your delete method, no problem
But please don't bother with any seperate appsettings or manually creating this stuff because an API works specifically with DIok, so I'm going to get a look into this DI and how does it works, I'm new to this
Feel free to ask about it once you read about it, and tried it
ok, thanks
I come from VB.NET and I wrote around 150 little projects (automating tools) with WinForms and I never used to structure the solution into more projects, folder, even classes, writing just the whole code into the main file, paradoxically without knowing objects
For this I thought to switch to C# starting from following a full course made of 40 videos, then starting with guided exercises, like this project
Hoping I'll be able to learn how to code good^^
Yeah for some reason VB, Winforms and .NET Framework never really did anything with DI. Ever since .NET Core it became relevant
I always coded in the .NET Framework 3.5 era
Welp
watched some videos about DI but not so much clear, maybe I'm missing something
wait, I decided to skip this for now by manually writing the connectionstring, this made the app working but on the console I still get printed the name of the namespace, so it does means that I was never reading GlobalContext content, even when the app didn't work
I think I'm doing it wrong somehow, I putted few breakpoints and I just realized I'm trying to read the connectionstring in the wrong way, I don't have to print GlobalContext but something other
that's what I tried to print, but trying printing GlobalContext.connectionString seems not working anyway
the class is set as public but maybe the "private" in the red square could be a problem
yes it was the reason cause I was unable to print it <:picard_facepalm:616692703685509130>
Accessibillity modifiers will never make you lose a value
They are strictly for development to indicate if you can access something or not
So your problem most definitely is not related to that because you will get a compiler error way before you get to the breakpoint 😛
In this case you most definitely use a different GlobalContext than what you instantiated, and I think you should really just get used to DI
Your database context should be a scoped dependency, which in this case means that it exists for as long as somebody's request is running. This will guarantee your have the same one everywhere as long as you set it up correctly
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.