/// <inheritdoc/>
        public override async Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull(nameof(messageWriter));
            }

            if (writeContext == null)
            {
                throw Error.ArgumentNull(nameof(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
                    };
                }

                await messageWriter.WriteEntityReferenceLinkAsync(entityReferenceLink).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        public async Task ReadJsonLightAsync()
        {
            // 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);
            await messageWriter.WriteEntityReferenceLinkAsync(new ODataEntityReferenceLink { Url = new Uri("http://localhost/samplelink") });

            ODataMessageReader messageReader = new ODataMessageReader(new MockODataRequestMessage(requestMessage),
                                                                      new ODataMessageReaderSettings(), model);

            IEdmNavigationProperty navigationProperty = GetNavigationProperty(model);

            var request = RequestFactory.Create("Get", "http://localhost", opt => opt.AddModel("odata", model));

            request.ODataFeature().PrefixName = "odata";
            ODataDeserializerContext context  = new ODataDeserializerContext
            {
                Request = request,
                Path    = new ODataPath(new NavigationPropertySegment(navigationProperty, navigationSource: null))
            };

            // Act
            Uri uri = await deserializer.ReadAsync(messageReader, typeof(Uri), context) as Uri;

            // Assert
            Assert.NotNull(uri);
            Assert.Equal("http://localhost/samplelink", uri.AbsoluteUri);
        }
 protected override async Task<Stream> WriteLinkContentAsync(string linkIdent)
 {
     var message = new ODataRequestMessage();
     using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
     {
         var link = new ODataEntityReferenceLink { Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkIdent) };
         await messageWriter.WriteEntityReferenceLinkAsync(link);
         return await message.GetStreamAsync();
     }
 }
        protected override async Task <Stream> WriteLinkContentAsync(string linkIdent)
        {
            var message = new ODataRequestMessage();

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var link = new ODataEntityReferenceLink {
                    Url = Utils.CreateAbsoluteUri(_session.Settings.UrlBase, linkIdent)
                };
                await messageWriter.WriteEntityReferenceLinkAsync(link);

                return(await message.GetStreamAsync());
            }
        }
Esempio n. 5
0
        protected async override Task <Stream> WriteLinkContentAsync(string method, string commandText, string linkIdent)
        {
            var message = IsBatch
                                ? await CreateBatchOperationMessageAsync(method, null, null, commandText, false).ConfigureAwait(false)
                                : new ODataRequestMessage();

            using var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model);
            var link = new ODataEntityReferenceLink
            {
                Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkIdent)
            };
            await messageWriter.WriteEntityReferenceLinkAsync(link).ConfigureAwait(false);

            return(IsBatch ? null : await message.GetStreamAsync().ConfigureAwait(false));
        }
        protected override async Task<Stream> WriteLinkContentAsync(string method, string commandText, string linkIdent)
        {
            var message = IsBatch
                ? await CreateBatchOperationMessageAsync(method, null, null, commandText, false)
                : new ODataRequestMessage();

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var link = new ODataEntityReferenceLink
                {
                    Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkIdent)
                };
                await messageWriter.WriteEntityReferenceLinkAsync(link);
                return IsBatch ? null : await message.GetStreamAsync();
            }
        }
Esempio n. 7
0
        /// <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 = GetEntityReferenceLink(graph);
                return(messageWriter.WriteEntityReferenceLinkAsync(entityReferenceLink));
            }

            return(TaskHelpers.Completed());
        }
Esempio n. 8
0
        public async Task ReadAsync_RoundTrips()
        {
            // Arrange
            IEdmModel model        = CreateModel();
            var       deserializer = new ODataEntityReferenceLinkDeserializer();
            MockODataRequestMessage    requestMessage = new MockODataRequestMessage();
            ODataMessageWriterSettings settings       = new ODataMessageWriterSettings()
            {
                ODataUri = new ODataUri {
                    ServiceRoot = new Uri("http://any/")
                }
            };

            settings.SetContentType(ODataFormat.Json);

            ODataMessageWriter messageWriter = new ODataMessageWriter(requestMessage, settings);
            await messageWriter.WriteEntityReferenceLinkAsync(new ODataEntityReferenceLink { Url = new Uri("http://localhost/samplelink") });

            var request = RequestFactory.Create("Get", "http://localhost", opt => opt.AddRouteComponents("odata", model));

            request.ODataFeature().RoutePrefix       = "odata";
            ODataMessageReaderSettings readSettings  = new ODataMessageReaderSettings();
            ODataMessageReader         messageReader = new ODataMessageReader(new MockODataRequestMessage(requestMessage), readSettings, model);
            ODataDeserializerContext   context       = new ODataDeserializerContext
            {
                Request = request,
                Path    = new ODataPath(new NavigationPropertySegment(GetNavigationProperty(model), navigationSource: null))
            };

            // Act
            Uri uri = await deserializer.ReadAsync(messageReader, typeof(Uri), context) as Uri;

            // Assert
            Assert.NotNull(uri);
            Assert.Equal("http://localhost/samplelink", uri.AbsoluteUri);
        }