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 Answers1

4

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.

Alexander
  • 9,104
  • 1
  • 17
  • 41
  • 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 – Fabian Wesling Nov 22 '20 at 12:10
  • "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. – Todd Menier Nov 23 '20 at 16:36
  • 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 – Fabian Wesling Nov 24 '20 at 23:11