删除任务/异步代码后,我收到以下异常:
{ "消息":"发生了错误。", " ExceptionMessage":"发生了一个或多个错误。", " ExceptionType":" System.AggregateException", " StackTrace":"在System.Threading.Tasks.Task
1.GetResultCore(Boolean waitCompletionNotification)\r\n at ABB.API.Customers.DynamicsCRM.Accounts.BusinessLogic.AccountService.GetAccount(String query, HttpRequestHeaders headers) in F:\\TFS\\Orian360 Res\\ABB.API\\Dev\\Customers\\DynamicsCRM\\Accounts\\BusinessLogic\\AccountService.cs:line 26\r\n at ABB.API.Customers.DynamicsCRM.Accounts.Web.AccountsController.GetAccount() in F:\\TFS\\Orian360 Res\\ABB.API\\Dev\\Customers\\DynamicsCRM\\Accounts\\Web\\Controllers\\AccountsController.cs:line 27\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary
2个参数,CancellationToken cancellationToken)\ r \ n ---从前一个位置开始的堆栈跟踪结束 抛出异常的地方--- \ r \ n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext(个)\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext(个)\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext(个)\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext(个)\ r \ n --- 从抛出异常的先前位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()&#34 ;, &#34; InnerException&#34;:{ &#34;消息&#34;:&#34;发生了错误。&#34;, &#34; ExceptionMessage&#34;:&#34;发送请求时发生错误。&#34;, &#34; ExceptionType&#34;:&#34; System.Net.Http.HttpRequestException&#34;, &#34; StackTrace&#34;:null, &#34; InnerException&#34;:{ &#34;消息&#34;:&#34;发生了错误。&#34;, &#34; ExceptionMessage&#34;:&#34;远程服务器返回错误:(401)未经授权。&#34;, &#34; ExceptionType&#34;:&#34; System.Net.WebException&#34;, &#34; StackTrace&#34;:&#34;在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\ r \ n 在System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult) AR)&#34 ;, &#34; InnerException&#34;:{ &#34;消息&#34;:&#34;发生了错误。&#34;, &#34; ExceptionMessage&#34;:&#34;目标主体名称不正确&#34;, &#34; ExceptionType&#34;:&#34; System.ComponentModel.Win32Exception&#34;, &#34; StackTrace&#34;:&#34;在System.Net.NTAuthentication.GetOutgoingBlob(Byte [] incomingBlob, Boolean throwOnError,SecurityStatus&amp; statusCode)\ r \ n at System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob)\ r \ n
在System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest,ICredentials凭证,布尔值 preAuthenticate)\ r \ n at System.Net.NegotiateClient.Authenticate(String challenge,WebRequest webRequest,ICredentials凭证)\ r \ n at System.Net.AuthenticationManagerDefault.Authenticate(String challenge, WebRequest请求,ICredentials凭据)\ r \ n at System.Net.AuthenticationState.AttemptAuthenticate(HttpWebRequest的 httpWebRequest,ICredentials authInfo)\ r \ n at System.Net.HttpWebRequest.CheckResubmitForAuth()\ r \ n at System.Net.HttpWebRequest.CheckResubmit(Exception&amp; e,Boolean&amp; disableUpload)&#34; } } }}
以前状态的代码没有问题:
public async Task<HttpResponseMessage> GetAccount(string query)
{
var response = Client.Instance.GetAsync(Client.Instance.BaseAddress + query);
var responseType = response.Result.StatusCode;
if (responseType == HttpStatusCode.NotFound)
{
return new HttpResponseMessage
{
StatusCode = responseType
};
}
return await response;
}
然后我将其更新为非异步:
public HttpResponseMessage GetAccount(string query, HttpRequestHeaders headers)
{
var requestUri = new Uri(Client.Instance.BaseAddress + query);
var request = new HttpRequestMessage
{
RequestUri = requestUri
};
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
var response = Client.Instance.SendAsync(request).Result;
return response;
}
导致此异常的原因是什么?它似乎与async / async有关?
控制器定义如下:
[HttpGet]
[Route("accounts({id:guid})")]
[Route("accounts")]
public Task<HttpResponseMessage> GetAccount()
{
var query = Request.RequestUri.AbsolutePath.Split('/').Last() + Request.RequestUri.Query;
var response = _accountService.GetAccount(query, Request.Headers);
return response;
}