C
C#2y ago
uselessxp

❔ 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
RubyNovaDev
RubyNovaDev2y ago
@uselessxp without code, we can't help you $paste
MODiX
MODiX2y ago
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!
uselessxp
uselessxp2y ago
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
RubyNovaDev
RubyNovaDev2y ago
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
uselessxp
uselessxp2y ago
on discord?
uselessxp
uselessxp2y ago
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!
uselessxp
uselessxp2y ago
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
RubyNovaDev
RubyNovaDev2y ago
yeah
Console.WriteLine("Hello world");
Console.WriteLine("Hello world");
@uselessxp what is the implementation of GlobalContext.ToString()?
uselessxp
uselessxp2y ago
what do you mean?
FusedQyou
FusedQyou2y ago
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 there
uselessxp
uselessxp2y ago
I 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?
FusedQyou
FusedQyou2y ago
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
uselessxp
uselessxp2y ago
yes I had to add the file manually
FusedQyou
FusedQyou2y ago
That aint gonna work Your library is not going to use that How are you using the library? Do you have an API?
uselessxp
uselessxp2y ago
I created the library, then I created the class Actions.cs and I imported it in the API project with "using"
FusedQyou
FusedQyou2y ago
And you referenced the library in the API? So your API has the appsettings. No point in having it in the library
uselessxp
uselessxp2y ago
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
FusedQyou
FusedQyou2y ago
No? You can do that totally fine lol
uselessxp
uselessxp2y ago
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
FusedQyou
FusedQyou2y ago
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.0
Dependency injection in ASP.NET Core
Learn how ASP.NET Core implements dependency injection and how to use it.
FusedQyou
FusedQyou2y ago
Actions should also be injected. This is not done.
uselessxp
uselessxp2y ago
it's not enough to create an instance of it for being able to use its method .Delete() ?
FusedQyou
FusedQyou2y ago
You are gonna have to get the configuration which required DI either way
uselessxp
uselessxp2y ago
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
FusedQyou
FusedQyou2y ago
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
uselessxp
uselessxp2y ago
ok, I'll get a look to DI, but currently microsoft documentation make hard understand also the easier thing 😱
FusedQyou
FusedQyou2y ago
What does your program.cs file look like?
uselessxp
uselessxp2y ago
Program.cs
FusedQyou
FusedQyou2y ago
builder.Services is your dependency collection, called IServiceCollection You inject it in there
uselessxp
uselessxp2y ago
this is the only difference between API and Library so I suppose that this code implement appsettings.json somehow
FusedQyou
FusedQyou2y ago
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
uselessxp
uselessxp2y ago
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
uselessxp
uselessxp2y ago
yeah I noticed it, there's not Program.cs in the library, just code
Messiah
Messiah2y ago
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
FusedQyou
FusedQyou2y ago
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
Messiah
Messiah2y ago
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
FusedQyou
FusedQyou2y ago
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
uselessxp
uselessxp2y ago
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
FusedQyou
FusedQyou2y ago
What he said doesn't make sense This is not related
uselessxp
uselessxp2y ago
ok
FusedQyou
FusedQyou2y ago
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 DI
uselessxp
uselessxp2y ago
ok, so I'm going to get a look into this DI and how does it works, I'm new to this
FusedQyou
FusedQyou2y ago
Feel free to ask about it once you read about it, and tried it
uselessxp
uselessxp2y ago
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^^
FusedQyou
FusedQyou2y ago
Yeah for some reason VB, Winforms and .NET Framework never really did anything with DI. Ever since .NET Core it became relevant
uselessxp
uselessxp2y ago
I always coded in the .NET Framework 3.5 era
FusedQyou
FusedQyou2y ago
Welp
uselessxp
uselessxp2y ago
watched some videos about DI but not so much clear, maybe I'm missing something
uselessxp
uselessxp2y ago
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
uselessxp
uselessxp2y ago
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
uselessxp
uselessxp2y ago
that's what I tried to print, but trying printing GlobalContext.connectionString seems not working anyway
uselessxp
uselessxp2y ago
the class is set as public but maybe the "private" in the red square could be a problem
uselessxp
uselessxp2y ago
yes it was the reason cause I was unable to print it <:picard_facepalm:616692703685509130>
FusedQyou
FusedQyou2y ago
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
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.