我试图根据新FW 4.5功能,如HttpClient的重写旧网络认证逻辑和等待/异步和我体验请求和响应之间的意外延迟(约15秒)。 我的猜测是,是因为客户端试图找到IE /使用代理就像是旧的HttpRequest / Web客户端。 下面的代码:
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 }; ;
}
}
}
这种方法是包裹着从API等方法,实际上什么也不做相关的当前情况:
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;
}
认证发生在各个视图模型的命令:
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");
}
}
调试输出:
[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
当我去掉UseProxy =在HttpClientHandler假设置,延迟去世和AUTH没有延迟。 问题是,当我不取消注释UseProxy(约每打一个运行运行)相同,有时甚至发生。 现在的问题是 - 这是一个bug还是什么? 试图调试,从服务器端,发现轮询请求之间没有差异。 提前致谢。