The code runs in .NET Standard 2.0. I have a constructor calling a method which will call an Azure function like this:
public ChatViewModel(IChatService chatService)
{
Task.Run(async () =>
{
if (!chatService.IsConnected)
{
await chatService.CreateConnection();
}
});
}
The method is like this:
public async Task CreateConnection()
{
await semaphoreSlim.WaitAsync();
if (httpClient == null)
{
httpClient = new HttpClient();
}
var result = await httpClient.GetStringAsync(uri);
var info = JsonConvert.DeserializeObject<Models.ConnectionInfo>(result);
//... some other code ...
semaphoreSlim.Release();
}
The code stops at
await httpClient.GetStringAsync(uri)
The URI is 100% valid, if I copy and paste it in the browser I get the JSON I wanted.
When opening up Fiddler, no call has been made to the URI.
EDIT Source code is coming from this Github repo: https://github.com/PacktPublishing/Xamarin.Forms-Projects/tree/master/Chapter06-07/Chat/Chat
And oddly enough I seem to get a call in Azure:
EDIT 2 This code is working:
static void Main(string[] args)
{
using (var httpClient = new HttpClient())
{
var a = httpClient.GetStringAsync(uri).GetAwaiter().GetResult();
}
}
This code is not working:
static void Main(string[] args)
{
Task.Run(async() => {
using (var httpClient = new HttpClient())
{
var a = await httpClient.GetStringAsync(uri);
}
});
}
private
, and instead have apublic static async
method that constructs the object for you. This way you can await the calls all the way through. – MickiemickleHttpClient
. Good call – MickiemickleWhen opening up Fiddler, no call has been made to the uri.
And oddly enough I seem to get a call in Azure:
I can't see how both of those things could be true. – Quinn