Exemplo n.º 1
0
        public static async Task WriteCollectionAsync(IEdmModel edmModel, ODataUri odataUri, IAsyncEnumerator <Object> asyncEnumerator, Stream responseStream)
        {
            var importSegment = (OperationImportSegment)odataUri.Path.LastSegment;
            IEdmOperationImport operationImport = importSegment.OperationImports.Single();
            Type returnType = edmModel.GetClrType(operationImport.Operation.ReturnType.Definition);

            IODataRequestMessage requestMessage = new Infrastructure.OeInMemoryMessage(responseStream, null);

            using (ODataMessageWriter messageWriter = new ODataMessageWriter(requestMessage,
                                                                             new ODataMessageWriterSettings()
            {
                EnableMessageStreamDisposal = false, ODataUri = odataUri
            }, edmModel))
            {
                IEdmTypeReference     typeRef = OeEdmClrHelper.GetEdmTypeReference(edmModel, returnType);
                ODataCollectionWriter writer  = await messageWriter.CreateODataCollectionWriterAsync(typeRef).ConfigureAwait(false);

                await writer.WriteStartAsync(new ODataCollectionStart()).ConfigureAwait(false);

                while (await asyncEnumerator.MoveNextAsync().ConfigureAwait(false))
                {
                    Object value = asyncEnumerator.Current;
                    if (value != null && value.GetType().IsEnum)
                    {
                        value = value.ToString();
                    }

                    await writer.WriteItemAsync(value).ConfigureAwait(false);
                }

                await writer.WriteEndAsync().ConfigureAwait(false);
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override async Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter,
                                                    ODataSerializerContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }

            if (writeContext == null)
            {
                throw Error.ArgumentNull("writeContext");
            }

            IEdmTypeReference collectionType = writeContext.GetEdmType(graph, type);

            Contract.Assert(collectionType != null);

            IEdmTypeReference     elementType = GetElementType(collectionType);
            ODataCollectionWriter writer      = await messageWriter.CreateODataCollectionWriterAsync(elementType);

            await WriteCollectionAsync(writer, graph, collectionType.AsCollection(), writeContext);
        }