本文整理汇总了C#中ODataMessageWriter类的典型用法代码示例。如果您正苦于以下问题:C# ODataMessageWriter类的具体用法?C# ODataMessageWriter怎么用?C# ODataMessageWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ODataMessageWriter类属于命名空间,在下文中一共展示了ODataMessageWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteObject
/// <inheridoc />
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");
}
if (graph != null)
{
ODataEntityReferenceLinks entityReferenceLinks = graph as ODataEntityReferenceLinks;
if (entityReferenceLinks == null)
{
IEnumerable<Uri> uris = graph as IEnumerable<Uri>;
if (uris == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
}
entityReferenceLinks = new ODataEntityReferenceLinks
{
Links = uris.Select(uri => new ODataEntityReferenceLink { Url = uri })
};
}
messageWriter.WriteEntityReferenceLinks(entityReferenceLinks);
}
}
示例2: ReadJsonLight
public void ReadJsonLight()
{
// Arrange
var deserializer = new ODataEntityReferenceLinkDeserializer();
MockODataRequestMessage requestMessage = new MockODataRequestMessage();
ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings();
writerSettings.SetContentType(ODataFormat.Json);
IEdmModel model = CreateModel();
ODataMessageWriter messageWriter = new ODataMessageWriter(requestMessage, writerSettings, model);
messageWriter.WriteEntityReferenceLink(new ODataEntityReferenceLink { Url = new Uri("http://localhost/samplelink") });
ODataMessageReader messageReader = new ODataMessageReader(new MockODataRequestMessage(requestMessage),
new ODataMessageReaderSettings(), model);
IEdmNavigationProperty navigationProperty = GetNavigationProperty(model);
ODataDeserializerContext context = new ODataDeserializerContext
{
Request = new HttpRequestMessage(),
Path = new ODataPath(new NavigationPathSegment(navigationProperty))
};
// Act
Uri uri = deserializer.Read(messageReader, typeof(Uri), context) as Uri;
// Assert
Assert.NotNull(uri);
Assert.Equal("http://localhost/samplelink", uri.AbsoluteUri);
}
示例3: 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");
}
IEdmNavigationSource navigationSource = writeContext.NavigationSource;
if (navigationSource == null)
{
throw new SerializationException(SRResources.NavigationSourceMissingDuringSerialization);
}
var path = writeContext.Path;
if (path == null)
{
throw new SerializationException(SRResources.ODataPathMissing);
}
ODataWriter writer = messageWriter.CreateODataEntryWriter(navigationSource, path.EdmType as IEdmEntityType);
WriteObjectInline(graph, navigationSource.EntityType().ToEdmTypeReference(isNullable: false), writer, writeContext);
}
示例4: WriteObject
/// <summary>
/// Writes the entity result to the response message.
/// </summary>
/// <param name="graph">The entity result to write.</param>
/// <param name="type">The type of the entity.</param>
/// <param name="messageWriter">The message writer.</param>
/// <param name="writeContext">The writing context.</param>
public override void WriteObject(
object graph,
Type type,
ODataMessageWriter messageWriter,
ODataSerializerContext writeContext)
{
RawResult rawResult = graph as RawResult;
if (rawResult != null)
{
graph = rawResult.Result;
type = rawResult.Type;
}
if (writeContext != null)
{
graph = RestierPrimitiveSerializer.ConvertToPayloadValue(graph, writeContext);
}
if (graph == null)
{
// This is to make ODataRawValueSerializer happily serialize null value.
graph = string.Empty;
}
base.WriteObject(graph, type, messageWriter, writeContext);
}
示例5: 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();
}
}
示例6: WriteCompletedAsyncResponse
public void WriteCompletedAsyncResponse()
{
var asyncWriter = this.TestInit();
var innerMessage = asyncWriter.CreateResponseMessage();
innerMessage.StatusCode = 200;
innerMessage.SetHeader("Content-Type", "application/json");
var settings = new ODataMessageWriterSettings();
settings.SetServiceDocumentUri(new Uri(ServiceDocumentUri));
settings.DisableMessageStreamDisposal = true;
using (var innerMessageWriter = new ODataMessageWriter(innerMessage, settings, this.userModel))
{
var entryWriter = innerMessageWriter.CreateODataEntryWriter(singleton, testType);
var entry = new ODataEntry() {TypeName = "NS.Test", Properties = new[] {new ODataProperty() {Name = "Id", Value = 1}}};
entryWriter.WriteStart(entry);
entryWriter.WriteEnd();
}
asyncWriter.Flush();
var payload = this.TestFinish();
Assert.Equal("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nOData-Version: 4.0\r\n\r\n{\"@odata.context\":\"http://host/service/$metadata#MySingleton\",\"Id\":1}", payload);
}
示例7: 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");
}
IEdmEntitySetBase entitySet = writeContext.NavigationSource as IEdmEntitySetBase;
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);
}
示例8: NonEntitySerializer
/// <summary>Initializes a new NonEntitySerializer instance.</summary>
/// <param name="requestDescription">Description of request.</param>
/// <param name="absoluteServiceUri">Base URI from which resources should be resolved.</param>
/// <param name="service">Service with configuration and provider from which metadata should be gathered.</param>
/// <param name="messageWriter">ODataMessageWriter instance which needs to be used to write the response.</param>
internal NonEntitySerializer(RequestDescription requestDescription, Uri absoluteServiceUri, IDataService service, ODataMessageWriter messageWriter)
: base(requestDescription, absoluteServiceUri, service, null)
{
Debug.Assert(messageWriter != null, "messageWriter != null");
this.writer = messageWriter;
}
示例9: 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();
}
示例10: CreateCollectionWriterWithTypeDefinitionAsItemType
public void CreateCollectionWriterWithTypeDefinitionAsItemType()
{
var writer = new ODataMessageWriter(new DummyRequestMessage());
var entityElementType = new EdmTypeDefinitionReference(new EdmTypeDefinition("NS", "Test", EdmPrimitiveTypeKind.Int32), false);
var collectionWriter = writer.CreateODataCollectionWriter(entityElementType);
Assert.IsNotNull(collectionWriter, "CreateODataCollectionWriter with type definition item type failed.");
}
示例11: CreateMessageWriterShouldSetAnnotationFilterWhenODataAnnotationIsSetOnPreferenceAppliedHeader
public void CreateMessageWriterShouldSetAnnotationFilterWhenODataAnnotationIsSetOnPreferenceAppliedHeader()
{
IODataResponseMessage responseMessage = new InMemoryMessage();
responseMessage.PreferenceAppliedHeader().AnnotationFilter = "*";
ODataMessageWriter writer = new ODataMessageWriter(responseMessage, new ODataMessageWriterSettings());
writer.Settings.ShouldIncludeAnnotation.Should().NotBeNull();
}
示例12: CreateCollectionWriterWithEnumAsItemType
public void CreateCollectionWriterWithEnumAsItemType()
{
var writer = new ODataMessageWriter(new DummyRequestMessage());
var entityElementType = new EdmEnumTypeReference(new EdmEnumType("FakeNS", "FakeEnum"), true);
var collectionWriter = writer.CreateODataCollectionWriter(entityElementType);
Assert.IsNotNull(collectionWriter, "CreateODataCollectionWriter with enum item type failed.");
}
示例13: CreateCollectionWriterWithEntityCollectionTypeShouldFail
public void CreateCollectionWriterWithEntityCollectionTypeShouldFail()
{
var writer = new ODataMessageWriter(new DummyRequestMessage());
var entityElementType = new EdmEntityTypeReference(new EdmEntityType("Fake", "Fake"), true);
Action createWriterWithEntityCollectionType = () => writer.CreateODataCollectionWriter(entityElementType);
createWriterWithEntityCollectionType.ShouldThrow<ODataException>().WithMessage(Strings.ODataMessageWriter_NonCollectionType("Fake.Fake"));
}
示例14: CreateCollectionWriterWithoutTypeShouldPassForJsonLight
public void CreateCollectionWriterWithoutTypeShouldPassForJsonLight()
{
var settings = new ODataMessageWriterSettings();
settings.SetContentType(ODataFormat.Json);
var writer = new ODataMessageWriter(new DummyRequestMessage(), settings, new EdmModel());
writer.CreateODataCollectionWriter(null).Should().BeOfType<ODataJsonLightCollectionWriter>();
}
示例15: WriteObjectAsync
/// <inheritdoc/>
public override Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (messageWriter == null)
{
throw Error.ArgumentNull("messageWriter");
}
if (writeContext == null)
{
throw Error.ArgumentNull("writeContext");
}
if (graph != null)
{
ODataEntityReferenceLink entityReferenceLink = graph as ODataEntityReferenceLink;
if (entityReferenceLink == null)
{
Uri uri = graph as Uri;
if (uri == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
}
entityReferenceLink = new ODataEntityReferenceLink { Url = uri };
}
messageWriter.WriteEntityReferenceLink(entityReferenceLink);
}
return Task.FromResult(true);
}