Http request hangs at GetAsync()

Http request hangs at GetAsync()

ricardo.salasHND82
Explorer Explorer
1,982 Views
3 Replies
Message 1 of 4

Http request hangs at GetAsync()

ricardo.salasHND82
Explorer
Explorer

Hey everyone,

 

I am trying to develop for the first time an integration with a third-party API, but when I try to execute a GET request Revit just locks and I have to terminate it through the Task Manager. I am not exactly sure why this is happening.

 

I already tried to perform the isolated request through a Console App, and it works.

 

When I try to debug it, everything seems to be working and all the values being assigned correctly, but it just hangs at the GetAsync() in the FohlioAPIConnection class.

 

The following is the related code:

 

 

 

 

[Transaction(TransactionMode.Manual)]
    internal class ExportData : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            HttpClient client = FohlioAPIConnection.InitializeClient();

            var getResponse = FohlioAPIConnection.ConnectionAsync(client, "projects");

            TaskDialog.Show("Result", getResponse.Result);

            return Result.Succeeded;

        }
        
    }

 

 

 

 

 

 

 

internal class FohlioAPIConnection
    {

        public static HttpClient InitializeClient()
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Environment.GetEnvironmentVariable("FOHLIO_TOKEN"));

            return client;
        }

        public static async Task<string> ConnectionAsync(HttpClient client, string endpoint)
        {

            HttpResponseMessage responseMessage = await client.GetAsync(new Uri("https://www.fohlio.com/openapi/projects"));

            var result = await responseMessage.Content.ReadAsStringAsync();

            return result;

        }
    }

 

 

 

 

0 Likes
1,983 Views
3 Replies
Replies (3)
Message 2 of 4

ricardo.salasHND82
Explorer
Explorer

It was the TaskDialog.Show(), it was misplaced. The Execute() in ExportData was being ended before FohlioAPIConnection.ConnectionAsync() was able of finishing, therefore the TaskDialog did not have anything to show, and it got stuck there.

 

Rookie's async mistake.

0 Likes
Message 3 of 4

ricaun
Advisor
Advisor

Usually, I use something like this to make an async function to force to wait for the result.

 

public static string Connection(HttpClient client, string endpoint)
{
    var task = Task.Run(async () =>
    {
        return await ConnectionAsync(client, endpoint);
    });
    return task.GetAwaiter().GetResult();
}

 

Something like this in your case, so you don't need to call Result.

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 4 of 4

ricardo.salasHND82
Explorer
Explorer

Interesting, will try it! Thank you so much for that tip!