示例#1
0
        public void WriteServiceDocument()
        {
            var msgWriter   = new ODataMessageWriter(_response, _writerSettings, _map.Model);
            var collections = new List <ODataResourceCollectionInfo>();

            foreach (
                var entityContainer in
                _map.Model.EntityContainers().Where(ec => _map.Model.IsDefaultEntityContainer(ec)))
            {
                foreach (var es in entityContainer.EntitySets())
                {
                    var collectionInfo = new ODataResourceCollectionInfo {
                        Url = new Uri(es.Name, UriKind.Relative)
                    };
                    var metadata = new AtomResourceCollectionMetadata {
                        Title = es.Name
                    };
                    collectionInfo.SetAnnotation(metadata);
                    collections.Add(collectionInfo);
                }
            }
            var workspace = new ODataWorkspace {
                Collections = collections
            };

            msgWriter.WriteServiceDocument(workspace);
        }
示例#2
0
        public override void WriteObject(object graph, ODataMessageWriter messageWriter, ODataSerializerWriteContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }

            messageWriter.WriteServiceDocument(graph as ODataWorkspace);
        }
        public dynamic Execute(dynamic parameters, INancyModule module)
        {
            NuGetODataModelBuilderODataPackage builder = new NuGetODataModelBuilderODataPackage();

            builder.Build();

            IODataResponseMessage      message        = new MemoryResponseMessage();
            ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings();

            var url = module.Request.Url.ToString();

            if (!url.EndsWith("/"))
            {
                url += "/";
            }

            writerSettings.BaseUri = new Uri(url);


            using (var msgWriter = new ODataMessageWriter(message, writerSettings, builder.Model))
            {
                var workspace = new ODataWorkspace
                {
                    Collections = new List <ODataResourceCollectionInfo>
                    {
                        new ODataResourceCollectionInfo
                        {
                            Name = "Packages",
                            Url  = new Uri("Packages", UriKind.Relative)
                        }
                    }
                };

                msgWriter.WriteServiceDocument(workspace);

                var msgStream = message.GetStream();

                msgStream.Seek(0, SeekOrigin.Begin);

                StreamReader reader = new StreamReader(msgStream);

                string text = reader.ReadToEnd();

                return(new Response
                {
                    ContentType = "application/xml; charset=utf-8",
                    Contents = contentStream =>
                    {
                        var byteData = Encoding.UTF8.GetBytes(text);
                        contentStream.Write(byteData, 0, byteData.Length);
                        msgStream.Dispose();
                        reader.Dispose();
                    }
                });
            }
        }
示例#4
0
        /// <inheritdoc/>
        public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }

            ODataServiceDocument serviceDocument = GetServiceDocument(graph, type);

            messageWriter.WriteServiceDocument(serviceDocument);
        }
        /// <inheritdoc/>
        public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }
            if (graph == null)
            {
                throw Error.ArgumentNull("graph");
            }

            ODataServiceDocument serviceDocument = graph as ODataServiceDocument;
            if (serviceDocument == null)
            {
                throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, type.Name));
            }

            messageWriter.WriteServiceDocument(serviceDocument);
        }
示例#6
0
        /// <inheritdoc/>
        public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }
            if (graph == null)
            {
                throw Error.ArgumentNull("graph");
            }

            ODataServiceDocument serviceDocument = graph as ODataServiceDocument;

            if (serviceDocument == null)
            {
                throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, type.Name));
            }

            messageWriter.WriteServiceDocument(serviceDocument);
        }
        /// <inheritdoc/>
        public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

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

            ODataServiceDocument serviceDocument = graph as ODataServiceDocument;

            if (serviceDocument == null)
            {
                throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, type?.Name));
            }

            // TODO: Call Async version?
            // messageWriter.WriteServiceDocumentAsync(serviceDocument);
            messageWriter.WriteServiceDocument(serviceDocument);
        }
示例#8
0
        static void Main(string[] args)
        {
            var filePath = (args.Length == 0) ? "trippin-csdl.xml" : args[0];
            var edmModel = LoadEdmModel(filePath);
            ODataServiceDocument serviceDoc = new ODataServiceDocument();

            serviceDoc = edmModel.GenerateServiceDocument();
            var messageWriterSettings = new ODataMessageWriterSettings();

            messageWriterSettings.ODataUri.ServiceRoot = new Uri("http://microsoft.com");
            var message = new InMemoryMessage()
            {
                Stream = new MemoryStream()
            };
            ODataMessageWriter writer = new ODataMessageWriter((IODataResponseMessage)message, messageWriterSettings);

            writer.WriteServiceDocument(serviceDoc);
            message.Stream.Seek(0, SeekOrigin.Begin);
            StreamReader reader = new StreamReader(message.Stream);

            File.WriteAllText(filePath.Replace(".xml", "-service-doc.xml"), reader.ReadToEnd());
            Console.Write("Service doc created");
            Console.ReadLine();
        }
示例#9
0
        private IEdmModel GetEdmModel2()
        {
            var       builder = new CustomModelBuilder();
            IEdmModel model   = builder
                                //.BuildAddressType()
                                //.BuildCategoryType()
                                //.BuildCustomerType()
                                .BuildWeatherForecastType()
                                .BuildDefaultContainer()
                                .BuildCustomerSet()
                                .GetModel();

            MemoryStream    stream  = new MemoryStream();
            InMemoryMessage message = new InMemoryMessage {
                Stream = stream
            };
            ODataMessageWriterSettings settings = new ODataMessageWriterSettings();

            settings.ODataUri = new ODataUri
            {
                ServiceRoot = new Uri("https://localhost:44329/odata/odata.svc/")
            };



            // write metadata payload
            //ODataMessageWriter writer = new ODataMessageWriter((IODataResponseMessage)message, settings,model);
            //writer.WriteMetadataDocument();


            // write services documents payload
            // One way
            //ODataMessageWriter writer = new ODataMessageWriter((IODataResponseMessage)message, settings);
            //ODataServiceDocument serviceDocument = new ODataServiceDocument();
            //serviceDocument.EntitySets = new[]
            //{
            //    new ODataEntitySetInfo
            //    {
            //        Name = "WeatherForecast",
            //        Title = "WeatherForecast",
            //        Url = new Uri("WeatherForecast", UriKind.Relative)
            //    },

            //};
            //writer.WriteServiceDocument(serviceDocument);

            // Second way
            ODataMessageWriter   writer          = new ODataMessageWriter((IODataResponseMessage)message, settings);
            ODataServiceDocument serviceDocument = model.GenerateServiceDocument();

            writer.WriteServiceDocument(serviceDocument);


            // write entity response
            //ODataMessageWriter writer = new ODataMessageWriter((IODataResponseMessage)message, settings,model);
            //IEdmEntitySet entitySet = model.FindDeclaredEntitySet("WeatherForecast");
            //ODataWriter odataWriter = writer.CreateODataResourceSetWriter(entitySet);
            //ODataResourceSet set = new ODataResourceSet();
            //odataWriter.WriteStart(set);
            //odataWriter.WriteEnd();


            // generating file

            message.Stream.Seek(0, SeekOrigin.Begin);
            StreamReader reader = new StreamReader(message.Stream);
            var          path   = Path.Combine(AppContext.BaseDirectory, "servicesdocument.xml");

            File.WriteAllText(path, reader.ReadToEnd());


            return(model);
        }
示例#10
0
 public void WriteServiceDocument()
 {
     var msgWriter = new ODataMessageWriter(_response, _writerSettings, _map.Model);
     var collections = new List<ODataResourceCollectionInfo>();
     foreach (
         var entityContainer in
             _map.Model.EntityContainers().Where(ec => _map.Model.IsDefaultEntityContainer(ec)))
     {
         foreach (var es in entityContainer.EntitySets())
         {
             var collectionInfo = new ODataResourceCollectionInfo {Url = new Uri(es.Name, UriKind.Relative)};
             var metadata = new AtomResourceCollectionMetadata {Title = es.Name};
             collectionInfo.SetAnnotation(metadata);
             collections.Add(collectionInfo);
         }
     }
     var workspace = new ODataWorkspace {Collections = collections};
     msgWriter.WriteServiceDocument(workspace);
 }