Ejemplo n.º 1
0
            private async ValueTask <(IEdmEntitySet entitySet, ODataResource resource)> ReadEntityFromStreamAsync(Stream content, Uri requestUrl, String contentType)
            {
                ODataUri      odataUri  = OeParser.ParseUri(_edmModel, _baseUri, requestUrl);
                IEdmEntitySet entitySet = ((EntitySetSegment)odataUri.Path.FirstSegment).EntitySet;

                _edmEntityType = entitySet.EntityType();
                IEdmModel edmModel = _edmModel.GetEdmModel(entitySet);

                ODataResource?       resource       = null;
                IODataRequestMessage requestMessage = new Infrastructure.OeInMemoryMessage(content, contentType, _serviceProvider);
                var settings = new ODataMessageReaderSettings
                {
                    ClientCustomTypeResolver    = ClientCustomTypeResolver,
                    EnableMessageStreamDisposal = false
                };

                using (var messageReader = new ODataMessageReader(requestMessage, settings, edmModel))
                {
                    ODataReader reader = await messageReader.CreateODataResourceReaderAsync(entitySet, entitySet.EntityType()).ConfigureAwait(false);

                    while (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        if (reader.State == ODataReaderState.ResourceEnd)
                        {
                            resource = (ODataResource)reader.Item;
                        }
                    }
                    if (resource == null)
                    {
                        throw new InvalidOperationException("operation not contain entry");
                    }
                }

                return(entitySet, resource);
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads a <see cref="ODataResource"/> or <see cref="ODataResourceSet"/> object.
        /// </summary>
        /// <param name="reader">The OData reader to read from.</param>
        /// <returns>The read resource or resource set.</returns>
        public static async Task <ODataItemWrapper> ReadResourceOrResourceSetAsync(this ODataReader reader)
        {
            if (reader == null)
            {
                throw Error.ArgumentNull(nameof(reader));
            }

            ODataItemWrapper         topLevelItem = null;
            Stack <ODataItemWrapper> itemsStack   = new Stack <ODataItemWrapper>();

            while (await reader.ReadAsync().ConfigureAwait(false))
            {
                ReadODataItem(reader, itemsStack, ref topLevelItem);
            }

            Contract.Assert(reader.State == ODataReaderState.Completed, "We should have consumed all of the input by now.");
            Contract.Assert(topLevelItem != null, "A top level resource or resource set should have been read by now.");
            return(topLevelItem);
        }
        public async Task VerifyCanReadResponseAsyncTest()
        {
            IODataResponseMessageAsync responseMessage = await new ClientHttpRequestMessage(new Uri(TestDemoService.ServiceBaseUri, "Products(1)")).GetResponseAsync();

            using (ODataMessageReader messageReader = new ODataMessageReader(
                       responseMessage,
                       new ODataMessageReaderSettings(),
                       TestUtils.GetServiceModel(TestDemoService.ServiceBaseUri)))
            {
                ODataReader reader = await messageReader.CreateODataEntryReaderAsync();

                while (await reader.ReadAsync())
                {
                    if (reader.State == ODataReaderState.EntryEnd)
                    {
                        ODataEntry entry = (ODataEntry)reader.Item;
                        Assert.AreEqual("DataServiceProviderDemo.Product", entry.TypeName);
                    }
                }
            }
        }