本文整理汇总了C#中ODataMessageWriter类的典型用法代码示例。如果您正苦于以下问题:C# ODataMessageWriter类的具体用法?C# ODataMessageWriter怎么用?C# ODataMessageWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ODataMessageWriter类属于命名空间,在下文中一共展示了ODataMessageWriter类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
}
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:33,代码来源:ODataEntityReferenceLinksSerializer.cs
示例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);
}
开发者ID:nicholaspei,项目名称:aspnetwebstack,代码行数:28,代码来源:ODataEntityReferenceLinkDeserializerTests.cs
示例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);
}
开发者ID:BarclayII,项目名称:WebApi,代码行数:29,代码来源:ODataEntityTypeSerializer.cs
示例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);
}
开发者ID:cinaradem,项目名称:RESTier,代码行数:33,代码来源:RestierRawSerializer.cs
示例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();
}
}
开发者ID:Swethach,项目名称:aspnetwebstack,代码行数:34,代码来源:ODataCollectionSerializer.cs
示例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);
}
开发者ID:larsenjo,项目名称:odata.net,代码行数:25,代码来源:ODataAsynchronousWriterTests.cs
示例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);
}
开发者ID:Gebov,项目名称:WebApi,代码行数:26,代码来源:ODataFeedSerializer.cs
示例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;
}
开发者ID:larsenjo,项目名称:odata.net,代码行数:12,代码来源:NonEntitySerializer.cs
示例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();
}
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:26,代码来源:ODataFeedSerializer.cs
示例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.");
}
开发者ID:rossjempson,项目名称:odata.net,代码行数:7,代码来源:ODataMessageWriterTests.cs
示例11: CreateMessageWriterShouldSetAnnotationFilterWhenODataAnnotationIsSetOnPreferenceAppliedHeader
public void CreateMessageWriterShouldSetAnnotationFilterWhenODataAnnotationIsSetOnPreferenceAppliedHeader()
{
IODataResponseMessage responseMessage = new InMemoryMessage();
responseMessage.PreferenceAppliedHeader().AnnotationFilter = "*";
ODataMessageWriter writer = new ODataMessageWriter(responseMessage, new ODataMessageWriterSettings());
writer.Settings.ShouldIncludeAnnotation.Should().NotBeNull();
}
开发者ID:rossjempson,项目名称:odata.net,代码行数:7,代码来源:ODataMessageWriterTests.cs
示例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.");
}
开发者ID:rossjempson,项目名称:odata.net,代码行数:7,代码来源:ODataMessageWriterTests.cs
示例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"));
}
开发者ID:rossjempson,项目名称:odata.net,代码行数:7,代码来源:ODataMessageWriterTests.cs
示例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>();
}
开发者ID:rossjempson,项目名称:odata.net,代码行数:7,代码来源:ODataMessageWriterTests.cs
示例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);
}
开发者ID:joshcomley,项目名称:WebApi,代码行数:32,代码来源:ODataEntityReferenceLinkSerializer.cs
示例16: WriteMessageAsync_ResponseContainsContentId_IfHasContentIdInRequestChangeSet
public void WriteMessageAsync_ResponseContainsContentId_IfHasContentIdInRequestChangeSet()
{
MemoryStream ms = new MemoryStream();
HttpContent content = new StringContent(String.Empty, Encoding.UTF8, "multipart/mixed");
content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", Guid.NewGuid().ToString()));
IODataResponseMessage odataResponse = new ODataMessageWrapper(ms, content.Headers);
var batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter();
HttpResponseMessage response = new HttpResponseMessage
{
Content = new StringContent("any", Encoding.UTF8, "text/example")
};
var request = new HttpRequestMessage();
var contentId = Guid.NewGuid().ToString();
request.SetODataContentId(contentId);
response.RequestMessage = request;
batchWriter.WriteStartBatch();
batchWriter.WriteStartChangeset();
ODataBatchResponseItem.WriteMessageAsync(batchWriter, response, CancellationToken.None).Wait();
batchWriter.WriteEndChangeset();
batchWriter.WriteEndBatch();
ms.Position = 0;
string result = new StreamReader(ms).ReadToEnd();
Assert.Contains("any", result);
Assert.Contains("text/example", result);
Assert.Contains("Content-ID", result);
Assert.Contains(contentId, result);
}
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:30,代码来源:ODataBatchResponseItemTest.cs
示例17: DetermineResponseFormat
/// <summary>
/// Determines the response format based on the results of content negotiation.
/// </summary>
/// <param name="payloadKind">The payload kind of the response.</param>
/// <param name="acceptableMediaTypes">
/// The acceptable media types used to determine the content type of the message.
/// This is a comma separated list of content types as specified in RFC 2616, Section 14.1
/// </param>
/// <param name="acceptableCharSets">
/// The acceptable charsets to use to the determine the encoding of the message.
/// This is a comma separated list of charsets as specified in RFC 2616, Section 14.2
/// </param>
/// <returns>The format the response should use. </returns>
internal ODataFormatWithParameters DetermineResponseFormat(ODataPayloadKind payloadKind, string acceptableMediaTypes, string acceptableCharSets)
{
Debug.Assert(payloadKind != ODataPayloadKind.Unsupported, "kind != ODataPayloadKind.Unsupported");
ContentNegotiationResponseMessage responseMessage = new ContentNegotiationResponseMessage();
ODataMessageWriterSettings settings = new ODataMessageWriterSettings { Version = this.responseVersion };
settings.EnableAtomSupport();
settings.SetContentType(acceptableMediaTypes, acceptableCharSets);
try
{
using (ODataMessageWriter writer = new ODataMessageWriter(responseMessage, settings))
{
ODataFormat format = ODataUtils.SetHeadersForPayload(writer, payloadKind);
return new ODataFormatWithParameters(format, responseMessage.ContentType);
}
}
catch (ODataContentTypeException exception)
{
if (this.throwIfNoMatch)
{
throw new DataServiceException(415, null, Microsoft.OData.Service.Strings.DataServiceException_UnsupportedMediaType, null, exception);
}
return null;
}
}
开发者ID:larsenjo,项目名称:odata.net,代码行数:41,代码来源:ResponseContentTypeNegotiator.cs
示例18: 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);
}
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:29,代码来源:ODataEntityReferenceLinkSerializer.cs
示例19: WriteObject_ThrowsArgumentNull_WriteContext
public void WriteObject_ThrowsArgumentNull_WriteContext()
{
ODataMessageWriter messageWriter = new ODataMessageWriter(new Mock<IODataRequestMessage>().Object);
Assert.ThrowsArgumentNull(
() => _serializer.WriteObject(graph: new object(), messageWriter: messageWriter, writeContext: null),
"writeContext");
}
开发者ID:balajivasudevan,项目名称:aspnetwebstack,代码行数:7,代码来源:ODataEntityTypeSerializerTests.cs
示例20: 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);
}
开发者ID:ahmetgoktas,项目名称:aspnetwebstack,代码行数:32,代码来源:ODataCollectionSerializerTests.cs
注:本文中的ODataMessageWriter类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论