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;
}
}
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;
}
}
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.
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.
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.
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.
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.