本文整理汇总了C#中System.Net.Http.Formatting.JsonMediaTypeFormatter.ReadFromStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# JsonMediaTypeFormatter.ReadFromStreamAsync方法的具体用法?C# JsonMediaTypeFormatter.ReadFromStreamAsync怎么用?C# JsonMediaTypeFormatter.ReadFromStreamAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.Formatting.JsonMediaTypeFormatter
的用法示例。
在下文中一共展示了JsonMediaTypeFormatter.ReadFromStreamAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HttpError_Roundtrips_WithJsonFormatter
public void HttpError_Roundtrips_WithJsonFormatter()
{
HttpError error = new HttpError("error") { { "ErrorCode", 42 }, { "Data", new[] { "a", "b", "c" } } };
MediaTypeFormatter formatter = new JsonMediaTypeFormatter();
MemoryStream stream = new MemoryStream();
formatter.WriteToStreamAsync(typeof(HttpError), error, stream, content: null, transportContext: null).Wait();
stream.Position = 0;
HttpError roundtrippedError = formatter.ReadFromStreamAsync(typeof(HttpError), stream, content: null, formatterLogger: null).Result as HttpError;
Assert.NotNull(roundtrippedError);
Assert.Equal("error", roundtrippedError.Message);
Assert.Equal(42L, roundtrippedError["ErrorCode"]);
JArray data = roundtrippedError["Data"] as JArray;
Assert.Equal(3, data.Count);
Assert.Contains("a", data);
Assert.Contains("b", data);
Assert.Contains("c", data);
}