问题描述
我正在使用VS 2012并将目标框架设置为4.0。
I am using VS 2012 and set the target framework as 4.0.
public async Task< HttpResponseMessage> GetResponse(Uri url)
{
this.httpClient.DefaultRequestHeaders.Accept.Clear(); &NBSP;&NBSP;&NBSP;
的 HttpResponseMessage响应=等待this.httpClient.GetAsync(URL);&NBSP; 强>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
response.EnsureSuccessStatusCode();
}
public async Task<HttpResponseMessage> GetResponse(Uri url)
{
this.httpClient.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await this.httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
}
在上面一行(标记为粗体),我收到以下错误:
On the above line (marked bold), i am getting following error:
-------------------------------------------------- ----------------
错误:
$
System.NullReferenceException未处理
HResult = - 2147467261
消息=对象引用未设置为对象的实例。
Source = Microsoft.CompilerServices.AsyncTargetingPack.Net4
StackTrace:
服务器堆栈跟踪:System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback
)在System.Runtime.CompilerS回调,对象状态)在System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback回调,对象状态)在System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback回调,对象状态) ervices.TaskAwaiter<> c__DisplayClassa< OnCompletedInternal> b__0(任务
参数0)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;在异常重新抛出[0]:在System.Runtime.CompilerServices.AsyncMethodBuilderCore< ThrowAsync> b__1(对象状态)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
在System.Threading.ExecutionContext.RunInternal(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException:
-------------------------------------- ------------------------
------------------------------------------------------------------
Error:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.CompilerServices.AsyncTargetingPack.Net4
StackTrace:
Server stack trace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext) at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state) at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state) at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClassa.<OnCompletedInternal>b__0(Task param0)
Exception rethrown at [0]: at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException:
--------------------------------------------------------------
虽然成功通话是为 await中传递的URL做出的this.httpClient.GetAsync(url)
因为我检查了fiddler中的调用详细信息并找到了成功的响应,但后来得到了上述错误。
$
我已根据
https://nuget.org/packages/Microsoft.Bcl.Async 但仍然得到相同的错误。此外,我已为Microsoft.CompilerServices.AsyncTargetingPack.Net4.dll安装了Microsoft.CompilerServices.AsyncTargetingPack.1.0.0。
Though the successfully call is making for the URL passed in await this.httpClient.GetAsync(url)as i checked the call detail in fiddler and found the successful response but then got the above mentioned error.
I have installed "Microsoft.Bcl.Async" as per the https://nuget.org/packages/Microsoft.Bcl.Async but still getting the same error. Also i have installed the Microsoft.CompilerServices.AsyncTargetingPack.1.0.0 for Microsoft.CompilerServices.AsyncTargetingPack.Net4.dll.
此错误的任何建议和解决方案。
Any suggessions and solutions for this error.
谢谢,
- JP Sharma
Thanks,
--JP Sharma
JP Sharma
JP Sharma
推荐答案
这篇关于等待使用httpClient.GetAsync(url)并获取错误System.NullReferenceException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!