glacasa / Mastonet

C# Library for Mastodon
MIT License
225 stars 36 forks source link

AuthenticationClient.CreateApp() hangs when used in an Avalonia UI project #104

Closed benpocalypse closed 11 months ago

benpocalypse commented 1 year ago

First - thanks for making this library, it's really awesome.

Description When using the Mastonet library in an Avalonia project, for some reason the AuthenticationClient.CreatApp() method hangs and never returns (even past the default 100 second HttpClient timeout.)

I traced the problem down to the Post() method and was able to change it slightly so that it runs correctly. I'm not an expert in HttpClient-fu, so I made a fork and will open a PR shortly with my suggested fix - but feel free to tell me my approach isn't correct.

This behavior doesn't happen when used outside of a project referencing Avalonia. Very odd.

EDIT: Pull request is here: https://github.com/glacasa/Mastonet/pull/105

13xforever commented 1 year ago

Looking at pr and the problem, seems like a pretty common issue with blocking the await when running on UI thread. You should consider running the code on a background thread and using Dispatcher.UIThread if you need to change UI for some reason from that thread. Also consider using .ConfigureAwait(false) that would enable resume from another thread, but that would also necessitate the use of Dispatcher.UIThread after the await block as it could run on a different thread after it was resumed.