2

I have a rest api with .net5, which has a global exception filter, and returns an ErrorMessage object in case of exceptions. I call the api from a WinForms .net framework 4.8 application and the badrequests from the server, from the client side I get a System.AggregateException exception and according to the flurl documentation it should be FlurlHttpException. I tried to make calls with HttpClient, and everything works as expected, but I find it easier to use flurl, so I want to find a solution to the problem, if someone has any idea how to do it and wants to share it would be great

        try
        {
            var result = (serverUrl)
                         .AppendPathSegment(endPoit)
                         .PostJsonAsync(new { Email = email, Password = password }).Result;
            if (result.ResponseMessage.IsSuccessStatusCode)
            {
                var aut = result.GetJsonAsync<Autorizacion>().Result;
            }
        }
        catch (FlurlHttpException ex)
        {
            var error = ex.GetResponseJsonAsync<ErrorMessage>();
        }

1 Answer 1

5

Firstly, prefer using async await instead of blocking Result call. But if you have to go with blocking calls then use GetAwaiter().GetResult() to get the unwrapped exception.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answering, your answer solved my problem. I'm new to the world of asynchronous calls, and in this case I can't use async await, but GetAwaiter (). GetResult () works great It turns out to be a concept error of mine, I could never get a flurl exception if I wait for the result of the task.resul. giant hug and thank you again
"in this case I can't use async await" Why not? Flurl is an async-only library. Using it the way you are is an invitation for deadlocks and I recommend either switching to an async programming model or using a library that explicitly supports blocking HTTP calls.
It was saying that I couldn't use async and await because I had to make several changes, but I ended up doing and now I use all asynchronous ... big hug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.