I'm trying to rewrite old network authentication logic according to new fw 4.5 features such as HttpClient and await/async and i experience unexpected latency (around 15s) between request and response. My guess is that happens because client tries to find/use proxy from IE like it was with old HttpRequest/WebClient. Here's the code :
public static async Task<AuthResult> GetDataFromServiceAsync(string url, string login, string password)
{
Debug.WriteLine("[" + DateTime.Now + "] GetDataFromServiceAsync");
var handler = new HttpClientHandler { Credentials = new NetworkCredential(login, password)/*, UseProxy = false*/ };
var client = new HttpClient(handler) { MaxResponseContentBufferSize = Int32.MaxValue };
try
{
var resp = await client.GetAsync(new Uri(url));
var content = resp.Content.ReadAsStringAsync().Result;
var auth = ParseContent(content);
Debug.WriteLine("[" + DateTime.Now + "] Returning AuthResult : " + auth);
return new AuthResult { Success = auth, Exception = null};
}
catch (Exception exception)
{
//
Debug.WriteLine("[" + DateTime.Now + "] Returning error AuthResult : " + exception.Message);
return new AuthResult { Success = false, Exception = exception }; ;
}
}
}
This method is wrapped with other method from api that actually does nothing relevant to current case :
public async Task<AuthResult> IsAuthenticated(string login, string password)
{
Debug.WriteLine("[" + DateTime.Now + "] Starting ISAuthenticationService.IsAuthenticated");
// ... (cut) ...
// async
var authResult = await ISHelpers.GetDataFromServiceAsync(url, login, password);
Debug.WriteLine("[" + DateTime.Now + "] Ending ISAuthenticationService.IsAuthenticated");
return authResult;
}
Authentication happens in respective ViewModel command :
private async void ExecuteAuthenticationCommand()
{
// Test stub
//var authService = new Helpers.MockupAuthenticationService();
var authService = new Helpers.ISAuthenticationService();
var auth = await authService.IsAuthenticated(Login, Password);
if (auth.Success)
{
MessageBox.Show("Authentication success");
Messenger.Default.Send<LoginDataItem>(_dataItem);
}
else
{
MessageBox.Show(auth.Exception.Message, "Incorrect login data");
}
}
Debug output :
[27.06.2012 16:54:10] Starting ISAuthenticationService.IsAuthenticated
[27.06.2012 16:54:10] GetDataFromServiceAsync
[27.06.2012 16:54:25] ParseContent in GetDataFromServiceAsync
[27.06.2012 16:54:25] Returning AuthResult : True
[27.06.2012 16:54:25] Ending ISAuthenticationService.IsAuthenticated
When i uncomment UseProxy = false in HttpClientHandler settings, latency passes away and auth has no delay. Problem is same happens sometimes even when i don't uncomment UseProxy (one run per about dozen runs). The question is - is that a bug or what? Tried to debug that from server-side, no differences between polling requests found. Thanks in advance.