Do anyone have a working example with async HttpClient in a DW/Rapido feed?
Developer forum
Call API using async C# HttpClient in DW/Rapido feed
Replies
You must be logged in to post in the forum
Do anyone have a working example with async HttpClient in a DW/Rapido feed?
Hi Martin,
I'm not sure I understand your question completely. Have you made your own WebApi controller or are you trying to use the HttpClient inside a Razor template?
If you have made your own controller, you can make an async action like this:
[Route("asynctest")] [HttpGet] public async System.Threading.Tasks.TaskAsyncTest() { var client = new System.Net.Http.HttpClient(); var response = await client.GetAsync("http://dynamicweb.com"); var content = await response.Content.ReadAsStringAsync(); return Ok(content); }
If you need to use an async method inside a Razor template, you need to make is synchronous. You can do that by doing this:
var client = new System.Net.Http.HttpClient(); var response = client.GetAsync("http://dynamicweb.com").GetAwaiter().GetResult(); var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
It's important to use GetAwaiter and GetResult instead of either .Wait or .Result as they have some strange behavior when dealing with exceptions.
I hope this helps.
- Jeppe
You must be logged in to post in the forum