예제 #1
0
        public static ODataEntry ReadODataEntry(EdmModel model, Stream stream)
        {
            ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings()
            {
                BaseUri = ServiceRootUri
            };
            var responseMessage = new ODataResponseMessage(stream);

            using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, model))
            {
                var reader = messageReader.CreateODataEntryReader();

                while (reader.Read())
                {
                    switch (reader.State)
                    {
                    case ODataReaderState.EntryEnd:
                        ODataEntry entry = reader.Item as ODataEntry;
                        return(entry);
                    }
                }
            }

            return(null);
        }
예제 #2
0
        private static void WriteODataEntry(IEdmModel model, Stream stream, SingletonSegment singletonSegment,
                                            ODataPath queryPath)
        {
            var entry = new ODataEntry()
            {
                Properties = new List <ODataProperty>()
                {
                    new ODataProperty()
                    {
                        Name  = "CompanyID",
                        Value = 1
                    },
                    new ODataProperty()
                    {
                        Name  = "Name",
                        Value = "Wonderland"
                    },
                    new ODataProperty()
                    {
                        Name  = "Revenue",
                        Value = 1000
                    }
                }
            };

            ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings();

            writerSettings.Version  = ODataVersion.V4;
            writerSettings.ODataUri = new ODataUri()
            {
                ServiceRoot = ServiceRootUri, Path = queryPath
            };
            writerSettings.PayloadBaseUri = ServiceRootUri;
            writerSettings.SetContentType("application/json;odata.metadata=full", Encoding.UTF8.WebName);
            writerSettings.AutoComputePayloadMetadataInJson = true;

            var message = new ODataResponseMessage(stream);

            using (var messageWriter = new ODataMessageWriter(message, writerSettings, model))
            {
                var entryWriter = messageWriter.CreateODataEntryWriter(singletonSegment.Singleton,
                                                                       singletonSegment.EdmType as EdmEntityType);

                entryWriter.WriteStart(entry);
                entryWriter.WriteEnd();
                entryWriter.Flush();
            }
        }