✅ How To Use C# To Access FastAPI Server?
I'm not sure if it's possible, maybe it is and I've just not learned it yet, but I use FastAPI for my application backends in languages other than C# as I've been learning the Avalonia Framework more. In that time, I've just been using
Microsoft.Data.Sqlite
relative databases to storage and access information. I want to switch to using my FastAPI backends instead, I just can't find a tutorial on how to do it. Any suggestions for a good tutorial or youtube videos? Thanks.33 Replies
so it's a web api?
yes, you can make web requests with C# with httpclient
You would access that API like you would any other API, in other words
oh ok. thanks!
it would be like the github version service I wrote that makes an http client call to the repo's latest releases. Just different in it's responses
I'd imagine so, yeah
awesome. tyvm
sorry to bother. Quick question. Again, I'm normally used to using JS and ReactJS and Electron stuff to access FastAPI backends, so this is new to me. So when I run my ReactJS frontend website, or ReactJS+Electron frontend web-app, it gets "hosted" on my local host which gives me an ip address to put in my FastAPI cors middleware allow_origins array. How would I "host" my avalonia application on the local host like that so that it can query the backend? Or do I just run it like normal and I'm overthinking it?
You... would not
The API needs to be hosted and available over the network
the server is
Yeah
it's on my desktop running
That's enough
oh ok. I thought I was overthinking it lol
CORS is a browser thing
so then I don't need cors_middleware for fastapi then for the application to query it?
Nope
cors is a browser-side security feature to prevent malicious scripts from accessing other sites on your behalf
as in, if you load a script on sketchywebsite.com that tries to make requests to google's apis your browser won't let it
got you got you
one moment because I think I'm doing something wrong
I'm created a default avalonia application using CommunityToolkit.MVVM. I'm just trying to change the greeting to the
"Hello, World!"
message that the frontend calls to get from the backend
.Resultyes that's wrong
I thought it was. I wasn't entirely sure what to put there to get the value back
async calls should be awaited
you can't do it in a property getter though, so you'll have to restructure your code
I do await the call inside of the try/catch
oh wait
I'm dumb
something like this
yes and no
constructors can't be async
right. let me look back at my todo app
if you need to get data asynchronously in order to construct a class, a good pattern is to make the constructor private and add an async factory method
Can't really have an async factor of an Avalonia window tho
I'm honestly not too sure. I don't normally practice my avalonia with asynch writing. and this is my first time not only doign that but using fastapi as a backend instead of a relative database that the application uses right there in the client folder inside of a
root/Services
folderAre you using ReactiveUI or Community Toolkit?
CommunityToolkit
so I had an idea, but it failed. I changed my code to this, however, when I run the application, it doesn't launch the app. It just sits there
that didn't solve the problem anyway
any use of Result is a big code smell unless you have already awaited the task
it's making the query. The console on the fastapi server shows that root is being accessed with code 200. It' just getting the frontend to do right lol
yea I'm trying to figure that part out with you guys. I'm just not that good at it
I don't have a command written in though? Unless I should make the greeting change on a button click?
this doesn't work either.
"Hello, Avalonia!"
displays on the screen instead of "Hello, World!"
from the server
that's my AppServer that I'm using to connect tot he fast apiI'll take a look tomorrow. Haven't used Avalonia much myself, let alone in an async context
ok I appreciate iti ❤️