我有一个与
Conference API project类似的项目(几乎相同),它采用与返回Collectionjson内容的着名项目类似的方法.我很难设置ReadDocument的Collection属性(第30行),因为它没有任何setter.我可以通过执行以下更改来绕过此问题
public CollectionjsonContent(Collection collection) { var serializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore,Formatting = Newtonsoft.Json.Formatting.Indented,ContractResolver = new CamelCasePropertyNamesContractResolver() }; collection.Version = "1.0"; Headers.ContentType = new MediaTypeHeaderValue("application/vnd.collection+json"); using (var writer = new JsonTextWriter(new StreamWriter(_memoryStream)){CloSEOutput = false}) { //var readDocument = new ReadDocument(); {IReadDocument.Collection = collection}; var serializer = JsonSerializer.Create(serializerSettings); serializer.Serialize(writer,collection); writer.Flush(); } _memoryStream.Position = 0; }
虽然上面的代码编译并在某种程度上解决了问题但是我将再次遇到另一个问题,即无法在我的控制器单元测试中使用JsonCollection内容.请考虑以下单元测试代码段:
using (var request = CreateRequest()) { var controller = new TestController(DataService) {Request = request}; var temp = await controller.ListAsync(gridSearchData,sampleSearchData); if ((temp is NotFoundResult) && (sampleCollection.Any())) { Assert.Fail("Controller did not return any result but query did"); } var json = await temp.ExecuteAsync(cancellationTokenSource); var readDocument = json.Content.ReadAsAsync<ReadDocument>(new[] {new CollectionjsonFormatter()},cancellationTokenSource).Result; }
由于我没有设置ReadDocument的集合属性,因此readDocument始终为空,我无法读取其内容.
如何在WEB API项目中异步读取客户端JsonCollection的内容?
要了解方法的清晰图片,请查看Conference Web Api
和authors blog
解决方法
好的,这已经修好了. Collection属性现在可以再次设置.
我刚刚用这个修复程序推动了release 0.7.0,一个主要的命名重构以及对序列化的一个很好的改进,而不是写出空集合.
请参阅release notes以了解更改(特别是命名,因为包名称和命名空间已更改)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。