本文整理汇总了C#中ObjectContent.ReadAsStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectContent.ReadAsStreamAsync方法的具体用法?C# ObjectContent.ReadAsStreamAsync怎么用?C# ObjectContent.ReadAsStreamAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectContent
的用法示例。
在下文中一共展示了ObjectContent.ReadAsStreamAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrimitiveTypesDeserializeAsOData
public void PrimitiveTypesDeserializeAsOData(string typeString, Type type, object value)
{
HttpConfiguration configuration = new HttpConfiguration();
ODataConventionModelBuilder model = new ODataConventionModelBuilder();
model.EntitySet<WorkItem>("WorkItems");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
ODataMediaTypeFormatter formatter = CreateFormatter(model.GetEdmModel(), request);
ObjectContent content = new ObjectContent(type, value, formatter, "application/xml");
var stream = content.ReadAsStreamAsync().Result;
Assert.Equal(
value,
formatter.ReadFromStreamAsync(type, stream, content, new Mock<IFormatterLogger>().Object).Result);
}
示例2: RoundTripUsingBson
public void RoundTripUsingBson()
{
//Arrange
var formatters = new[] { new JsonNetFormatter() };
var fromContact = new Contact { FirstName = "Brad", LastName = "Abrams" };
// Act
var fromContent = new ObjectContent<Contact>(fromContact, "application/bson", formatters);
var contentReadStream = fromContent.ReadAsStreamAsync().Result;
var newStream = new MemoryStream();
contentReadStream.Position = 0;
contentReadStream.CopyTo(newStream);
newStream.Position = 0;
var toContent = new ObjectContent<Contact>(new StreamContent(newStream), formatters);
toContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");
var toContact = toContent.ReadAsAsync<Contact>().Result;
//Arrange
Assert.AreEqual(fromContact.FirstName, toContact.FirstName);
Assert.AreEqual(fromContact.LastName, toContact.LastName);
}