سؤال

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.

هل كانت مفيدة؟

المحلول

This is not a bug. IE's default setting is to try and auto-detect the proxy, which can take up to 30 seconds. To disable automatic detection you need to set UseProxy to False.

In fact, the settings are not really related to IE. It's just that IE uses (and sets) the system's default settings. HttpClient and WebClient both use the system's default settings unless you override them.

As for the detection speed, it depends on the system settings. If you disable automatic proxy detection in IE or Chrome you will notice that your browser opens much faster the first time after a reboot. This is because it doesn't try to detect the proxy. The proxy detection process is described in Automatic Proxy Detection and involves several steps:

  1. Locate the last used proxy configuration script
  2. Discover the proxy from DHCP
  3. Lookup a machine called WAPD using DNS

Steps #2 and #3 can take a long time, depending on your network infrastructure and may even involve timeouts.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top