本文整理汇总了C#中ObjectContent.ReadAsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectContent.ReadAsAsync方法的具体用法?C# ObjectContent.ReadAsAsync怎么用?C# ObjectContent.ReadAsAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectContent
的用法示例。
在下文中一共展示了ObjectContent.ReadAsAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadAsAsyncOfT_WhenContentIsObjectContent_GoesThroughSerializationCycleToConvertTypes
public void ReadAsAsyncOfT_WhenContentIsObjectContent_GoesThroughSerializationCycleToConvertTypes()
{
var content = new ObjectContent<int[]>(new int[] { 10, 20, 30, 40 }, new JsonMediaTypeFormatter());
byte[] result = content.ReadAsAsync<byte[]>().Result;
Assert.Equal(new byte[] { 10, 20, 30, 40 }, result);
}
示例2: WhenUsedToDeserializeShouldCreateCorrectObject
public async void WhenUsedToDeserializeShouldCreateCorrectObject()
{
var formatter = new ProtoBufFormatter();
var item = new Item { Id = 1, Name = "Filip" };
var content = new ObjectContent<Item>(item, formatter);
var deserializedItem = await content.ReadAsAsync<Item>(new[] {formatter});
Assert.Same(item, deserializedItem);
}
示例3: ReadAsAsync_WhenContentIsObjectContentAndValueIsNotCompatibleType_SerializesAndDeserializesValue
public void ReadAsAsync_WhenContentIsObjectContentAndValueIsNotCompatibleType_SerializesAndDeserializesValue()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(TestClass))).Returns(true);
_formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
var value = new TestClass();
var content = new ObjectContent<TestClass>(value, _formatterMock.Object, _mediaType.MediaType);
SetupUpRoundTripSerialization(type => new TestClass());
Assert.Throws<InvalidCastException>(() => content.ReadAsAsync<string>(_formatters).RethrowFaultedTaskException());
Assert.IsNotType<string>(content.ReadAsAsync(typeof(string), _formatters).Result);
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content.Headers, It.IsAny<IFormatterLogger>()), Times.Exactly(2));
}
示例4: SetupUpRoundTripSerialization
public void ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNotNullable_SerializesAndDeserializesValue()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(object))).Returns(true);
_formatterMock.Setup(f => f.CanReadType(typeof(Int32))).Returns(true);
var content = new ObjectContent<object>(null, _formatterMock.Object, _mediaType.MediaType);
SetupUpRoundTripSerialization();
Assert.IsType<Int32>(content.ReadAsAsync<Int32>(_formatters).Result);
Assert.IsType<Int32>(content.ReadAsAsync(typeof(Int32), _formatters).Result);
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content.Headers, It.IsAny<IFormatterLogger>()), Times.Exactly(2));
}
示例5: ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNullable_SerializesAndDeserializesValue
public void ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNullable_SerializesAndDeserializesValue()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(object))).Returns(true);
_formatterMock.Setup(f => f.CanReadType(It.IsAny<Type>())).Returns(true);
var content = new ObjectContent<object>(null, _formatterMock.Object);
SetupUpRoundTripSerialization(type => null);
Assert.Null(content.ReadAsAsync<object>(_formatters).Result);
Assert.Null(content.ReadAsAsync<TestClass>(_formatters).Result);
Assert.Null(content.ReadAsAsync<Nullable<int>>(_formatters).Result);
Assert.Null(content.ReadAsAsync(typeof(object), _formatters).Result);
Assert.Null(content.ReadAsAsync(typeof(TestClass), _formatters).Result);
Assert.Null(content.ReadAsAsync(typeof(Nullable<int>), _formatters).Result);
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content.Headers, It.IsAny<IFormatterLogger>()), Times.Exactly(6));
}
示例6: ReadAsAsync_WhenContentIsObjectContentAndValueIsCompatibleType_ReadsValueFromObjectContent
public void ReadAsAsync_WhenContentIsObjectContentAndValueIsCompatibleType_ReadsValueFromObjectContent()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(TestClass))).Returns(true);
var value = new TestClass();
var content = new ObjectContent<TestClass>(value, _formatterMock.Object);
Assert.Same(value, content.ReadAsAsync<object>(_formatters).Result);
Assert.Same(value, content.ReadAsAsync<TestClass>(_formatters).Result);
Assert.Same(value, content.ReadAsAsync(typeof(object), _formatters).Result);
Assert.Same(value, content.ReadAsAsync(typeof(TestClass), _formatters).Result);
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content.Headers, It.IsAny<IFormatterLogger>()), Times.Never());
}