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);
            }
        /// <inheritdoc />
        public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw new ArgumentNullException(nameof(messageReader));
            }

            if (readContext == null)
            {
                throw new ArgumentNullException(nameof(readContext));
            }

            IEdmTypeReference edmType = readContext.GetEdmType(type);

            Contract.Assert(edmType != null);

            if (!edmType.IsStructured())
            {
                throw Error.Argument("type", SRResources.ArgumentMustBeOfType, "Structured");
            }

            IEdmStructuredTypeReference structuredType = edmType.AsStructured();

            IEdmNavigationSource navigationSource = null;

            if (structuredType.IsEntity())
            {
                if (readContext.Path == null)
                {
                    throw Error.Argument("readContext", SRResources.ODataPathMissing);
                }

                navigationSource = readContext.Path.GetNavigationSource();
                if (navigationSource == null)
                {
                    throw new SerializationException(SRResources.NavigationSourceMissingDuringDeserialization);
                }
            }

            ODataReader odataReader = await messageReader
                                      .CreateODataResourceReaderAsync(navigationSource, structuredType.StructuredDefinition()).ConfigureAwait(false);

            ODataResourceWrapper topLevelResource = await odataReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false)
                                                    as ODataResourceWrapper;

            Contract.Assert(topLevelResource != null);

            return(ReadInline(topLevelResource, structuredType, readContext));
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw Error.ArgumentNull("messageReader");
            }

            IEdmStructuredTypeReference structuredType   = GetStructuredType(type, readContext);
            IEdmNavigationSource        navigationSource = GetNavigationSource(structuredType, readContext);
            ODataReader odataReader = await messageReader.CreateODataResourceReaderAsync(navigationSource, structuredType.StructuredDefinition());

            ODataResourceWrapper topLevelResource = await odataReader.ReadResourceOrResourceSetAsync() as ODataResourceWrapper;

            Contract.Assert(topLevelResource != null);

            return(ReadInline(topLevelResource, structuredType, readContext));
        }
Esempio n. 4
0
        public async Task ReadResourceInResponseMessageAsync()
        {
            var payload = @"HTTP/1.1 200 OK
Content-Type: application/json
OData-Version: 4.0

{""@odata.context"":""http://tempuri.org/$metadata#Customers/$entity"",""Id"":1,""Name"":""Customer 1""}";

            await SetupRawInputContextAndRunTestAsync(
                payload,
                async (rawInputContext) =>
            {
                var asynchronousReader = await rawInputContext.CreateAsynchronousReaderAsync();
                var responseMessage    = await asynchronousReader.CreateResponseMessageAsync();

                Assert.Equal(200, responseMessage.StatusCode);
                Assert.Contains(new KeyValuePair <string, string>("Content-Type", "application/json"), responseMessage.Headers);
                Assert.Contains(new KeyValuePair <string, string>("OData-Version", "4.0"), responseMessage.Headers);

                using (var messageReader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), this.model))
                {
                    var jsonLightReader = await messageReader.CreateODataResourceReaderAsync(this.customerEntitySet, this.customerEntityType);

                    await DoReadAsync(
                        jsonLightReader as ODataJsonLightReader,
                        verifyResourceAction: (resource) =>
                    {
                        Assert.NotNull(resource);
                        Assert.Equal("NS.Customer", resource.TypeName);
                        var properties = resource.Properties.ToArray();
                        Assert.Equal(2, properties.Length);
                        Assert.Equal("Id", properties[0].Name);
                        Assert.Equal(1, properties[0].Value);
                        Assert.Equal("Name", properties[1].Name);
                        Assert.Equal("Customer 1", properties[1].Value);
                    });
                }
            });
        }