本文整理汇总了C#中System.Web.Http.OData.Formatter.Serialization.ODataSerializerContext类的典型用法代码示例。如果您正苦于以下问题:C# ODataSerializerContext类的具体用法?C# ODataSerializerContext怎么用?C# ODataSerializerContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ODataSerializerContext类属于System.Web.Http.OData.Formatter.Serialization命名空间,在下文中一共展示了ODataSerializerContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteObject
public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (graph == null)
{
throw Error.ArgumentNull("graph");
}
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
ODataError oDataError = graph as ODataError;
if (oDataError == null)
{
HttpError httpError = graph as HttpError;
if (httpError == null)
{
string message = Error.Format(SRResources.ErrorTypeMustBeODataErrorOrHttpError, graph.GetType().FullName);
throw new SerializationException(message);
}
else
{
oDataError = httpError.ToODataError();
}
}
bool includeDebugInformation = oDataError.InnerError != null;
messageWriter.WriteError(oDataError, includeDebugInformation);
}
示例2: WriteObjectInline
/// <inheritdoc />
public override void WriteObjectInline(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
if (writer == null)
{
throw Error.ArgumentNull("writer");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
if (graph == null)
{
throw new SerializationException(Error.Format(SRResources.CannotSerializerNull, Feed));
}
IEnumerable enumerable = graph as IEnumerable; // Data to serialize
if (enumerable == null)
{
throw new SerializationException(
Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
}
WriteFeed(enumerable, writer, writeContext);
}
示例3: WriteObject
public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
IEdmEntitySet entitySet = writeContext.EntitySet;
if (entitySet == null)
{
throw new SerializationException(SRResources.EntitySetMissingDuringSerialization);
}
IEdmNavigationProperty navigationProperty = GetNavigationProperty(writeContext.Path);
if (navigationProperty == null)
{
throw new SerializationException(SRResources.NavigationPropertyMissingDuringSerialization);
}
messageWriter.WriteEntityReferenceLink(new ODataEntityReferenceLink { Url = graph as Uri }, entitySet,
navigationProperty);
}
示例4: WriteObject
/// <inheritdoc/>
public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
ODataCollectionWriter writer = messageWriter.CreateODataCollectionWriter();
writer.WriteStart(
new ODataCollectionStart
{
Name = writeContext.RootElementName
});
ODataProperty property = CreateProperty(graph, writeContext.RootElementName, writeContext);
if (property != null)
{
ODataCollectionValue collectionValue = property.Value as ODataCollectionValue;
foreach (object item in collectionValue.Items)
{
writer.WriteItem(item);
}
writer.WriteEnd();
writer.Flush();
}
}
示例5: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType
public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
{
// Arrange
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var cars = builder.EntitySet<Car>("cars");
var paintAction = cars.EntityType.Action("Paint");
IEdmModel model = builder.GetEdmModel();
var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataRoute(routeName, null, model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
request.SetODataRouteName(routeName);
var serializerContext = new ODataSerializerContext { Model = model, EntitySet = carsEdmSet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carsEdmSet.ElementType.AsReference(), new Car { Model = 2009, Name = "Accord" });
// Act
Uri link = ActionLinkGenerationConvention.GenerateActionLink(entityContext, paintAction);
Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
}
示例6: 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));
}
示例7: CanConfigureAllLinksViaIdLink
public void CanConfigureAllLinksViaIdLink()
{
// Arrange
ODataModelBuilder builder = GetCommonModel();
var expectedEditLink = "http://server/service/Products(15)";
var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
products.HasIdLink(c =>
string.Format(
"http://server/service/Products({0})",
c.GetPropertyValue("ID")
),
followsConventions: false);
var actor = builder.EntitySets.Single();
var model = builder.GetEdmModel();
var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
var productsSet = model.SchemaElements.OfType<IEdmEntityContainer>().Single().EntitySets().Single();
var productInstance = new EntitySetLinkConfigurationTest_Product { ID = 15 };
var serializerContext = new ODataSerializerContext { Model = model, EntitySet = productsSet };
var entityContext = new EntityInstanceContext(serializerContext, productType.AsReference(), productInstance);
var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);
// Act
var selfLinks = entitySetLinkBuilderAnnotation.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.Default);
// Assert
Assert.NotNull(selfLinks.EditLink);
Assert.Equal(expectedEditLink, selfLinks.EditLink.ToString());
Assert.NotNull(selfLinks.ReadLink);
Assert.Equal(expectedEditLink, selfLinks.ReadLink.ToString());
Assert.NotNull(selfLinks.IdLink);
Assert.Equal(expectedEditLink, selfLinks.IdLink);
}
示例8: WriteObject
public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
IEdmEntitySet entitySet = writeContext.EntitySet;
if (entitySet == null)
{
throw new SerializationException(SRResources.EntitySetMissingDuringSerialization);
}
// No null check; entity type is not required for successful serialization.
IEdmEntityType entityType = _edmElementType;
ODataWriter writer = messageWriter.CreateODataFeedWriter(entitySet, entityType);
WriteObjectInline(graph, writer, writeContext);
writer.Flush();
}
示例9: WriteObject_Calls_CreateODataComplexValue
public void WriteObject_Calls_CreateODataComplexValue()
{
// Arrange
MemoryStream stream = new MemoryStream();
IODataResponseMessage message = new ODataMessageWrapper(stream);
ODataMessageWriter messageWriter = new ODataMessageWriter(message);
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);
}
示例10: Apply_Doesnot_Override_UserConfiguration
public void Apply_Doesnot_Override_UserConfiguration()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var vehicles = builder.EntitySet<Vehicle>("vehicles");
var car = builder.AddEntity(typeof(Car));
var paintAction = vehicles.EntityType.Action("Paint");
paintAction.HasActionLink(ctxt => new Uri("http://localhost/ActionTestWorks"), followsConventions: false);
ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();
convention.Apply(paintAction, builder);
IEdmModel model = builder.GetEdmModel();
var vehiclesEdmSet = model.EntityContainers().Single().FindEntitySet("vehicles");
var carEdmType = model.FindDeclaredType("System.Web.Http.OData.Builder.TestModels.Car") as IEdmEntityType;
var paintEdmAction =
model.GetAvailableProcedures(
model.FindDeclaredType("System.Web.Http.OData.Builder.TestModels.Car") as IEdmEntityType).Single()
as IEdmAction;
Assert.NotNull(paintEdmAction);
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapODataRoute(model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(paintEdmAction);
var serializerContext = new ODataSerializerContext { Model = model, EntitySet = vehiclesEdmSet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carEdmType.AsReference(), new Car { Model = 2009, Name = "Accord" });
Uri link = actionLinkBuilder.BuildActionLink(entityContext);
Assert.Equal("http://localhost/ActionTestWorks", link.AbsoluteUri);
}
示例11: WriteObject
/// <inheritdoc />
public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
IEdmEntitySet entitySet = writeContext.EntitySet;
if (entitySet == null)
{
throw new SerializationException(SRResources.EntitySetMissingDuringSerialization);
}
IEdmTypeReference feedType = writeContext.GetEdmType(graph, type);
Contract.Assert(feedType != null);
IEdmEntityTypeReference entityType = GetEntityType(feedType);
ODataWriter writer = messageWriter.CreateODataFeedWriter(entitySet, entityType.EntityDefinition());
WriteObjectInline(graph, feedType, writer, writeContext);
}
示例12: ODataFeedSerializerTests
public ODataFeedSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers");
_customers = new[] {
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
},
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 42,
}
};
_customersType = new EdmCollectionTypeReference(
new EdmCollectionType(
new EdmEntityTypeReference(
_customerSet.ElementType,
isNullable: false)),
isNullable: false);
_writeContext = new ODataSerializerContext() { EntitySet = _customerSet, Model = _model };
}
示例13: CopyCtor_CopiesAllTheProperties
public void CopyCtor_CopiesAllTheProperties()
{
// Arrange
ODataSerializerContext context = new ODataSerializerContext
{
EntitySet = new Mock<IEdmEntitySet>().Object,
IsNested = true,
MetadataLevel = ODataMetadataLevel.FullMetadata,
Model = new Mock<IEdmModel>().Object,
Path = new ODataPath(),
Request = new HttpRequestMessage(),
RootElementName = "somename",
SelectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true),
SkipExpensiveAvailabilityChecks = true,
Url = new UrlHelper()
};
// Act
ODataSerializerContext result = new ODataSerializerContext(context);
// Assert
// Check each and every property. This test should fail if someone accidentally adds a new property and
// forgets to update the copy constructor.
foreach (PropertyInfo property in typeof(ODataSerializerContext).GetProperties())
{
if (property.PropertyType.IsValueType)
{
Assert.Equal(property.GetValue(context), property.GetValue(result));
}
else
{
Assert.Same(property.GetValue(context), property.GetValue(result));
}
}
}
示例14: WriteObject
public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
if (graph == null)
{
throw new SerializationException(Error.Format(Properties.SRResources.CannotSerializerNull,
ODataFormatterConstants.Entry));
}
IEdmEntitySet entitySet = writeContext.EntitySet;
if (entitySet == null)
{
throw new SerializationException(SRResources.EntitySetMissingDuringSerialization);
}
// No null check; entity type is not required for successful serialization.
IEdmEntityType entityType = _edmEntityTypeReference.Definition as IEdmEntityType;
ODataWriter writer = messageWriter.CreateODataEntryWriter(entitySet, entityType);
WriteObjectInline(graph, writer, writeContext);
writer.Flush();
}
示例15: Ctor_ThatBuildsNestedContext_CopiesProperties
public void Ctor_ThatBuildsNestedContext_CopiesProperties()
{
// Arrange
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
ODataSerializerContext context = new ODataSerializerContext
{
EntitySet = model.Customers,
MetadataLevel = ODataMetadataLevel.FullMetadata,
Model = model.Model,
Path = new ODataPath(),
Request = new HttpRequestMessage(),
RootElementName = "somename",
SelectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true),
SkipExpensiveAvailabilityChecks = true,
Url = new UrlHelper()
};
EntityInstanceContext entity = new EntityInstanceContext { SerializerContext = context };
SelectExpandClause selectExpand = new SelectExpandClause(new SelectItem[0], allSelected: true);
IEdmNavigationProperty navProp = model.Customer.NavigationProperties().First();
// Act
ODataSerializerContext nestedContext = new ODataSerializerContext(entity, selectExpand, navProp);
// Assert
Assert.Equal(context.MetadataLevel, nestedContext.MetadataLevel);
Assert.Same(context.Model, nestedContext.Model);
Assert.Same(context.Path, nestedContext.Path);
Assert.Same(context.Request, nestedContext.Request);
Assert.Equal(context.RootElementName, nestedContext.RootElementName);
Assert.Equal(context.SkipExpensiveAvailabilityChecks, nestedContext.SkipExpensiveAvailabilityChecks);
Assert.Same(context.Url, nestedContext.Url);
}