1 public async Task<UserDto[]> GetUsersByBlogIds(int[] blogIds) 2 { 3 var response = await _httpClient.PostAsJsonAsync<int[]>("/users/[blogids]", blogIds); 4 5 if (response.StatusCode != HttpStatusCode.OK) 6 { 7 var responseError = await response.Content.ReadAsAsync<HttpError>(); 8 Logger.Default.Error("WebApi_Error", response.RequestMessage.RequestUri + "发生了内部异常。\r\n" + 9 responseError.ExceptionMessage + "\r\n" + responseError.StackTrace); 10 } 11 return await response.Content.ReadAsAsync<UserDto[]>(); 12 }
该方法通过blogIds获取users信息,在运行到第7行时抛空引用异常,由于本地无法调试,不知道有没有好的解决办法?
注释掉异常部分的代码后,新的问题如下:
[ArgumentNullException: 值不能为 null。
参数名: inner]
System.Linq.Enumerable.Join(IEnumerable`1 outer, IEnumerable`1 inner, Func`2 outerKeySelector, Func`2 innerKeySelector, Func`3 resultSelector) +269
出现上述问题的代码如下:
var answers = _answerRepository.GetHomeworkAnswers(homeworkId).ToList();
var members = answers.Select(x => x.Answerer).GroupBy(x => new
{
x.MemberId,
x.BlogId
}).Select(x => x.Key).ToList();
var blogIds = members.Select(x => x.BlogId).ToArray();
var users = await ucenterService.GetUsersByBlogIds(blogIds);
return (from a in answers //此处抛出异常
join m in members on a.AnswererId equals m.MemberId
join u in users on m.BlogId equals u.BlogId
orderby a.DateAdded descending
select new AnswerDto
{
Title = a.Title,
Url = a.Url,
BlogUrl = u.BlogUrl,
DateAdded = a.DateAdded
})
.ToList();
把 response.Content.ReadAsAsync<HttpError>()
改为 response.Content.ReadAsStringAsync()
现在和问题补充的异常一样,我本地模拟数据时是没有问题的,唯一的不同是users数据通过调用webapi来获取的
@JoiWilliam: 那写日志看var answers = _answerRepository.GetHomeworkAnswers(homeworkId).ToList();
的值是什么?
@dudu: 果然answers没取到数据,竟然是一个参数传递错了😭
ReadAsAsync<> 目前貌似没作用了,只能ReadAsStringAsync 然后序列化
var responseError = await response.Content.ReadAsAsync>();
这里response
中可能没有包含异常信息,response.StatusCode
是多少?
输出异常信息.