首页 新闻 会员 周边 捐助

WebApi调用ReadAsAsync<HttpError>()时未将对象引用设置到对象的实例

0
悬赏园豆:10 [已解决问题] 解决于 2016-07-03 19:25
 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();

brusw的主页 brusw | 小虾三级 | 园豆:746
提问于:2016-07-03 16:15
< >
分享
最佳答案
0

response.Content.ReadAsAsync<HttpError>() 改为 response.Content.ReadAsStringAsync()

收获园豆:6
dudu | 高人七级 |园豆:29570 | 2016-07-03 17:39

现在和问题补充的异常一样,我本地模拟数据时是没有问题的,唯一的不同是users数据通过调用webapi来获取的

brusw | 园豆:746 (小虾三级) | 2016-07-03 18:01

@JoiWilliam: 那写日志看var answers = _answerRepository.GetHomeworkAnswers(homeworkId).ToList();的值是什么?

dudu | 园豆:29570 (高人七级) | 2016-07-03 18:08

@dudu: 果然answers没取到数据,竟然是一个参数传递错了😭

brusw | 园豆:746 (小虾三级) | 2016-07-03 19:18

ReadAsAsync<> 目前貌似没作用了,只能ReadAsStringAsync 然后序列化

Y2zz | 园豆:393 (菜鸟二级) | 2017-05-10 15:11
其他回答(2)
0

var responseError = await response.Content.ReadAsAsync>();

这里response中可能没有包含异常信息,response.StatusCode是多少?

收获园豆:4
jackccc | 园豆:1341 (小虾三级) | 2016-07-03 16:30
0

输出异常信息.

吴瑞祥 | 园豆:29449 (高人七级) | 2016-07-03 16:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册