我正在开发一个可以使用Http Client与我的服务器通信的WPF应用程序。
My Application build on .NET 4.我通过nugget安装Http Client。我也安装 通过nugget Microsoft.CompilerServices.AsyncTargetingPack 。
在Windows 8中,它没有任何问题。但在Windows 7中,我得到了一个例外
应用程序:whost.Generator.Creator.exe Framework版本:v4.0.30319 描述:由于未处理的异常,进程终止。 异常信息:System.Net.Http.HttpRequestException Stack:
服务器堆栈跟踪:
在[0]处重新抛出异常:at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 whost.Generator.Creator.Basic.ServerCommunicator.d__10.MoveNext()
在[1]重新抛出异常:at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 whost.Generator.Creator.ViewModel.LicenseGenerateViewModel.d_ 3.MoveNext() 在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.b _0(System.Object的) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object,Int32)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object的, System.Delegate,System.Object,Int32,System.Delegate)at System.Windows.Threading.DispatcherOperation.InvokeImpl()at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object的) 在System.Threading.ExecutionContext.runTryCode(System.Object)at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode,System.Object)at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object)at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object,Boolean)at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object)at System.Windows.Threading.DispatcherOperation.Invoke()at System.Windows.Threading.Dispatcher.ProcessQueue()at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr,Int32,IntPtr, Int.Ptr,布尔ByRef)在MS.Win32.HwndWrapper.WndProc(IntPtr, Int32,IntPtr,IntPtr,Boolean ByRef)at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)at at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object,Int32)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object的, System.Delegate,System.Object,Int32,System.Delegate)at System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan,System.Delegate,System.Object,Int32)at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr,Int32,IntPtr,IntPtr)
在 MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 在 System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) 在System.Windows.Threading.Dispatcher.Run()处 System.Windows.Application.RunDispatcher(System.Object)at System.Windows.Application.RunInternal(System.Windows.Window)at System.Windows.Application.Run(System.Windows.Window)at System.Windows.Application.Run()at whost.Generator.Creator.App.Main()
我不明白为什么我会得到这个例外。 我的服务器基于MVC 4和WEB API构建。
提前致谢。
更新1: 我的代码是:
var handler = new HttpClientHandler();
var cookieContainer = new CookieContainer();
handler.CookieContainer = cookieContainer;
var client = new HttpClient(handler) { BaseAddress = new Uri("http://myhost.com/") };
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var loginModel = new LoginModel
{
UserName = Username,
Password = Password,
};
var userLoginResposnce = await client.PostAsJsonAsync("account/Login", loginModel);
var successInfoTamplate = new { Success = true };
var loginResponceString = await userLoginResposnce.Content.ReadAsStringAsync();
var isSuccess = JsonConvert.DeserializeAnonymousType(loginResponceString, successInfoTamplate);
我在Windows 7 PC上安装了.NET 4。