示例#1
0
        public static IEnumerable <ODataEntry> ReadODataFeed(IEdmModel model, Stream stream)
        {
            ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings()
            {
                BaseUri = ServiceRootUri
            };
            var responseMessage = new ODataResponseMessage(stream);

            using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, model))
            {
                List <ODataEntry> entries = new List <ODataEntry>();
                var reader = messageReader.CreateODataFeedReader();

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

                        break;

                    default:
                        break;
                    }
                }
            }
        }
示例#2
0
        public static void WriteODataFeed(IEdmModel model, Stream stream)
        {
            // Create a ODataEntry
            var entry = new ODataEntry()
            {
                Properties = new List <ODataProperty>()
                {
                    new ODataProperty
                    {
                        Name = "ProductId", Value = 100
                    },
                    new ODataProperty
                    {
                        Name = "Name", Value = "The World"
                    },
                    new ODataProperty
                    {
                        Name = "SkinColor", Value = new ODataEnumValue("Green", DefaultNamespace + ".Color")
                    },
                    new ODataProperty
                    {
                        Name = "UserAccess", Value = new ODataEnumValue("ReadWrite, Execute", DefaultNamespace + ".AccessLevel")
                    }
                }
            };

            ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings();

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

            var responseMessage = new ODataResponseMessage(stream);

            using (var messageWriter = new ODataMessageWriter(responseMessage, writerSettings, model))
            {
                IEdmEntitySet entitySet  = model.FindDeclaredEntitySet("Products");
                ODataWriter   feedWriter = messageWriter.CreateODataFeedWriter(entitySet);

                var feed = new ODataFeed {
                    Id = new Uri(ServiceRootUri, "Products")
                };
                feedWriter.WriteStart(feed);
                feedWriter.WriteStart(entry);
                feedWriter.WriteEnd();
                feedWriter.WriteEnd();
                feedWriter.Flush();
            }
        }