本文整理汇总了C#中ODataMessageWrapper类的典型用法代码示例。如果您正苦于以下问题:C# ODataMessageWrapper类的具体用法?C# ODataMessageWrapper怎么用?C# ODataMessageWrapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ODataMessageWrapper类属于命名空间,在下文中一共展示了ODataMessageWrapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ODataEntityReferenceLinkSerializer_Serializes_UrisAndEntityReferenceLinks
public void ODataEntityReferenceLinkSerializer_Serializes_UrisAndEntityReferenceLinks(object uris)
{
// Arrange
ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer();
ODataSerializerContext writeContext = new ODataSerializerContext();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings
{
ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/") }
};
settings.SetContentType(ODataFormat.Json);
ODataMessageWriter writer = new ODataMessageWriter(message, settings);
// Act
serializer.WriteObject(uris, typeof(ODataEntityReferenceLinks), writer, writeContext);
stream.Seek(0, SeekOrigin.Begin);
string result = new StreamReader(stream).ReadToEnd();
// Assert
Assert.Equal("{\"@odata.context\":\"http://any/$metadata#Collection($ref)\"," +
"\"value\":[{\"@odata.id\":\"http://uri1/\"},{\"@odata.id\":\"http://uri2/\"}]}",
result);
}
示例2: Can_deserialize_payload_with_primitive_parameters
public void Can_deserialize_payload_with_primitive_parameters()
{
string actionName = "Primitive";
int quantity = 1;
string productCode = "PCode";
string body = "{" + string.Format(@" ""Quantity"": {0} , ""ProductCode"": ""{1}"" ", quantity, productCode) + "}";
ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body));
message.SetHeader("Content-Type", "application/json;odata=verbose");
IEdmModel model = GetModel();
ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model);
ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
ODataPath path = CreatePath(model, actionName);
ODataDeserializerContext context = new ODataDeserializerContext { Path = path, Model = model };
ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters;
Assert.NotNull(payload);
Assert.Same(
model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Primitive"),
ODataActionPayloadDeserializer.GetFunctionImport(context));
Assert.True(payload.ContainsKey("Quantity"));
Assert.Equal(quantity, payload["Quantity"]);
Assert.True(payload.ContainsKey("ProductCode"));
Assert.Equal(productCode, payload["ProductCode"]);
}
示例3: WriteObject_Calls_CreateODataComplexValue
public void WriteObject_Calls_CreateODataComplexValue()
{
// Arrange
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
settings.SetServiceDocumentUri(new Uri("http://any/"));
settings.SetContentType(ODataFormat.Atom);
ODataMessageWriter messageWriter = new ODataMessageWriter(message, settings);
Mock<ODataComplexTypeSerializer> serializer = new Mock<ODataComplexTypeSerializer>(new DefaultODataSerializerProvider());
ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "ComplexPropertyName", Model = _model };
object graph = new object();
ODataComplexValue complexValue = new ODataComplexValue
{
TypeName = "NS.Name",
Properties = new[] { new ODataProperty { Name = "Property1", Value = 42 } }
};
serializer.CallBase = true;
serializer.Setup(s => s.CreateODataComplexValue(graph, It.Is<IEdmComplexTypeReference>(e => e.Definition == _addressType), writeContext))
.Returns(complexValue).Verifiable();
// Act
serializer.Object.WriteObject(graph, typeof(Address), messageWriter, writeContext);
// Assert
serializer.Verify();
stream.Seek(0, SeekOrigin.Begin);
XElement element = XElement.Load(stream);
Assert.Equal("value", element.Name.LocalName);
Assert.Equal("#NS.Name", element.Attributes().Single(a => a.Name.LocalName == "type").Value);
Assert.Equal(1, element.Descendants().Count());
Assert.Equal("42", element.Descendants().Single().Value);
Assert.Equal("Property1", element.Descendants().Single().Name.LocalName);
}
示例4: Can_deserialize_payload_with_complex_parameters
public void Can_deserialize_payload_with_complex_parameters()
{
string actionName = "Complex";
string body = @"{ ""Quantity"": 1 , ""Address"": { ""StreetAddress"":""1 Microsoft Way"", ""City"": ""Redmond"", ""State"": ""WA"", ""ZipCode"": 98052 } }";
ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body));
message.SetHeader("Content-Type", "application/json;odata=verbose");
IEdmModel model = GetModel();
ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model);
ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(typeof(ODataActionParameters), new DefaultODataDeserializerProvider(model));
string url = "http://server/service/Customers(10)/" + actionName;
HttpRequestMessage request = GetPostRequest(url);
ODataDeserializerContext context = new ODataDeserializerContext { Request = request, Model = model };
ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters;
Assert.NotNull(payload);
Assert.Same(model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Complex"), payload.GetFunctionImport(context));
Assert.True(payload.ContainsKey("Quantity"));
Assert.Equal(1, payload["Quantity"]);
Assert.True(payload.ContainsKey("Address"));
MyAddress address = payload["Address"] as MyAddress;
Assert.NotNull(address);
Assert.Equal("1 Microsoft Way", address.StreetAddress);
Assert.Equal("Redmond", address.City);
Assert.Equal("WA", address.State);
Assert.Equal(98052, address.ZipCode);
}
示例5: WriteObject_WritesValueReturnedFrom_CreateODataCollectionValue
public void WriteObject_WritesValueReturnedFrom_CreateODataCollectionValue()
{
// Arrange
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriter messageWriter = new ODataMessageWriter(message);
Mock<ODataCollectionSerializer> serializer = new Mock<ODataCollectionSerializer>(new DefaultODataSerializerProvider());
ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "CollectionName", Model = _model };
IEnumerable enumerable = new object[0];
ODataCollectionValue collectionValue = new ODataCollectionValue { TypeName = "NS.Name", Items = new[] { 0, 1, 2 } };
serializer.CallBase = true;
serializer
.Setup(s => s.CreateODataCollectionValue(enumerable, It.Is<IEdmTypeReference>(e => e.Definition == _edmIntType.Definition), writeContext))
.Returns(collectionValue).Verifiable();
// Act
serializer.Object.WriteObject(enumerable, typeof(int[]), messageWriter, writeContext);
// Assert
serializer.Verify();
stream.Seek(0, SeekOrigin.Begin);
XElement element = XElement.Load(stream);
Assert.Equal("CollectionName", element.Name.LocalName);
Assert.Equal(3, element.Descendants().Count());
Assert.Equal(new[] { "0", "1", "2" }, element.Descendants().Select(e => e.Value));
}
示例6: WriteObject_WritesValueReturnedFrom_CreateODataCollectionValue
public void WriteObject_WritesValueReturnedFrom_CreateODataCollectionValue()
{
// Arrange
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings()
{
ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/") }
};
settings.SetContentType(ODataFormat.Json);
ODataMessageWriter messageWriter = new ODataMessageWriter(message, settings);
Mock<ODataCollectionSerializer> serializer = new Mock<ODataCollectionSerializer>(new DefaultODataSerializerProvider());
ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "CollectionName", Model = _model };
IEnumerable enumerable = new object[0];
ODataCollectionValue collectionValue = new ODataCollectionValue { TypeName = "NS.Name", Items = new[] { 0, 1, 2 } };
serializer.CallBase = true;
serializer
.Setup(s => s.CreateODataCollectionValue(enumerable, It.Is<IEdmTypeReference>(e => e.Definition == _edmIntType.Definition), writeContext))
.Returns(collectionValue).Verifiable();
// Act
serializer.Object.WriteObject(enumerable, typeof(int[]), messageWriter, writeContext);
// Assert
serializer.Verify();
stream.Seek(0, SeekOrigin.Begin);
string result = new StreamReader(stream).ReadToEnd();
Assert.Equal("{\"@odata.context\":\"http://any/$metadata#Collection(Edm.Int32)\",\"value\":[0,1,2]}", result);
}
示例7: Can_deserialize_payload_with_primitive_collection_parameters
public void Can_deserialize_payload_with_primitive_collection_parameters()
{
string actionName = "PrimitiveCollection";
string body = @"{ ""Name"": ""Avatar"", ""Ratings"": [ 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 ] }";
int[] expectedRatings = new int[] { 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 };
ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body));
message.SetHeader("Content-Type", "application/json;odata=verbose");
IEdmModel model = GetModel();
ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model);
ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(typeof(ODataActionParameters), new DefaultODataDeserializerProvider(model));
string url = "http://server/service/EntitySet(key)/" + actionName;
HttpRequestMessage request = GetPostRequest(url);
ODataDeserializerContext context = new ODataDeserializerContext { Request = request, Model = model };
ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters;
Assert.NotNull(payload);
Assert.Same(model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "PrimitiveCollection"), payload.GetFunctionImport(context));
Assert.True(payload.ContainsKey("Name"));
Assert.Equal("Avatar", payload["Name"]);
Assert.True(payload.ContainsKey("Ratings"));
IList<int> ratings = payload["Ratings"] as IList<int>;
Assert.Equal(10, ratings.Count);
Assert.True(expectedRatings.Zip(ratings, (expected, actual) => expected - actual).All(diff => diff == 0));
}
示例8: WriteObject_Calls_CreateODataComplexValue
public void WriteObject_Calls_CreateODataComplexValue()
{
// Arrange
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings
{
ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/"), }
};
settings.SetContentType(ODataFormat.Json);
ODataMessageWriter messageWriter = new ODataMessageWriter(message, settings);
Mock<ODataComplexTypeSerializer> serializer = new Mock<ODataComplexTypeSerializer>(new DefaultODataSerializerProvider());
ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "ComplexPropertyName", Model = _model };
object graph = new object();
ODataComplexValue complexValue = new ODataComplexValue
{
TypeName = "NS.Name",
Properties = new[] { new ODataProperty { Name = "Property1", Value = 42 } }
};
serializer.CallBase = true;
serializer.Setup(s => s.CreateODataComplexValue(graph, It.Is<IEdmComplexTypeReference>(e => e.Definition == _addressType), writeContext))
.Returns(complexValue).Verifiable();
// Act
serializer.Object.WriteObject(graph, typeof(Address), messageWriter, writeContext);
// Assert
serializer.Verify();
stream.Seek(0, SeekOrigin.Begin);
string result = new StreamReader(stream).ReadToEnd();
Assert.Equal("{\"@odata.context\":\"http://any/$metadata#NS.Name\",\"Property1\":42}", result);
}
示例9: ODataWorkspaceSerializer_Works
public void ODataWorkspaceSerializer_Works()
{
// Arrange
ODataWorkspaceSerializer serializer = new ODataWorkspaceSerializer();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
// Act
serializer.WriteObject(new ODataWorkspace(), _workspaceType, new ODataMessageWriter(message), new ODataSerializerContext());
// Assert
stream.Seek(0, SeekOrigin.Begin);
XElement element = XElement.Load(stream);
Assert.Equal("service", element.Name.LocalName);
}
示例10: ODataErrorSerializer_Works
public void ODataErrorSerializer_Works()
{
// Arrange
ODataErrorSerializer serializer = new ODataErrorSerializer();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataError error = new ODataError { Message = "Error!!!" };
// Act
serializer.WriteObject(error, new ODataMessageWriter(message), new ODataSerializerContext());
// Assert
stream.Seek(0, SeekOrigin.Begin);
XElement element = XElement.Load(stream);
Assert.Equal("Error!!!", element.Descendants().Single(e => e.Name.LocalName == "message").Value);
}
示例11: ODataMetadataSerializer_Works
public void ODataMetadataSerializer_Works()
{
// Arrange
ODataMetadataSerializer serializer = new ODataMetadataSerializer();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
IEdmModel model = new EdmModel();
// Act
serializer.WriteObject("42", typeof(IEdmModel), new ODataMessageWriter(message, settings, model), new ODataSerializerContext());
// Assert
stream.Seek(0, SeekOrigin.Begin);
XElement element = XElement.Load(stream);
Assert.Equal("Edmx", element.Name.LocalName);
}
示例12: ODataWorkspaceSerializer_Works
public void ODataWorkspaceSerializer_Works()
{
// Arrange
ODataServiceDocumentSerializer serializer = new ODataServiceDocumentSerializer();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
settings.SetServiceDocumentUri(new Uri("http://any/"));
settings.SetContentType(ODataFormat.Atom);
ODataMessageWriter writer = new ODataMessageWriter(message, settings);
// Act
serializer.WriteObject(new ODataServiceDocument(), _workspaceType, writer, new ODataSerializerContext());
// Assert
stream.Seek(0, SeekOrigin.Begin);
XElement element = XElement.Load(stream);
Assert.Equal("service", element.Name.LocalName);
}
示例13: ODataErrorSerializer_Works
public void ODataErrorSerializer_Works()
{
// Arrange
ODataErrorSerializer serializer = new ODataErrorSerializer();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataError error = new ODataError { Message = "Error!!!" };
ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
settings.SetContentType(ODataFormat.Json);
ODataMessageWriter writer = new ODataMessageWriter(message, settings);
// Act
serializer.WriteObject(error, typeof(ODataError), writer, new ODataSerializerContext());
stream.Seek(0, SeekOrigin.Begin);
string result = new StreamReader(stream).ReadToEnd();
// Assert
Assert.Equal("{\"error\":{\"code\":\"\",\"message\":\"Error!!!\"}}", result);
}
示例14: ODataMetadataSerializer_Works_ForSingleton
public void ODataMetadataSerializer_Works_ForSingleton()
{
// Arrange
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Singleton<Customer>("Me");
builder.EntitySet<Order>("MyOrders");
IEdmModel model = builder.GetEdmModel();
ODataMetadataSerializer serializer = new ODataMetadataSerializer();
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
// Act
serializer.WriteObject(model, typeof(IEdmModel), new ODataMessageWriter(message, settings, model), new ODataSerializerContext());
// Assert
stream.Seek(0, SeekOrigin.Begin);
string result = new StreamReader(stream).ReadToEnd();
Assert.Contains("<Singleton Name=\"Me\" Type=\"System.Web.OData.Formatter.Serialization.Models.Customer\">", result);
}
示例15: Read_Primitive
public void Read_Primitive(object obj)
{
// Arrange
IEdmPrimitiveTypeReference primitive = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer(primitive);
ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer(primitive);
MemoryStream stream = new MemoryStream();
ODataMessageWrapper message = new ODataMessageWrapper(stream);
serializer.WriteObject(
obj,
new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), EdmCoreModel.Instance),
new ODataSerializerContext { RootElementName = "Property" });
stream.Seek(0, SeekOrigin.Begin);
Assert.Equal(
obj,
deserializer.Read(
new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), EdmCoreModel.Instance),
new ODataDeserializerContext()));
}