Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Http request hangs at GetAsync()

3 REPLIES 3
Reply
Message 1 of 4
ricardo.salasHND82
1313 Views, 3 Replies

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

Http request hangs at GetAsync()

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;

        }
    }

 

 

 

 

3 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

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.

Message 3 of 4
ricaun
in reply to: ricardo.salasHND82

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

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
in reply to: ricaun

ricardo.salasHND82
Explorer
Explorer

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

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report