locked
Return OData Atom Feed using WCF Web API RRS feed

  • Question

  • I am trying to return an Atom feed from a WCF Web API and am looking for some help.  I'm using the ODataLib to try and help and have tried to base my code on the ODataLibSamples.

    I have a method like:

    [WebInvoke(UriTemplate = "{AuthenticateKey}/{System_Code}", Method = "GET")] public HttpResponseMessage Get(string AuthenticateKey, string System_Code) { HTTPClientRequestMessage message = new HTTPClientRequestMessage("http://www.abc.com/"); ODataFormat formatKind = ODataFormat.Atom; //message.SetHeader("Accept", formatKind == ODataFormat.Json ? "application/json" : "application/atom+xml"); message.Method = Microsoft.Data.OData.HttpMethod.Post; //message.SetHeader("MaxDataServiceVersion", maxVersion.ToHeaderValue()); message.SetHeader("MaxDataServiceVersion", "3"); // create the writer, indent for readability of the examples. ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings() { Indent = true, //pretty printing CheckCharacters = false, //sets this flag on the XmlWriter for ATOM BaseUri = new Uri("http://www.abc.com/"),//set the base uri to use in relative links Version = ODataVersion.V3 //set the Odata version to use when writing the entry }; writerSettings.SetContentType(formatKind); //create message writing for the message using (ODataMessageWriter messageWriter = new ODataMessageWriter(message, writerSettings)) { //creates a streaming writer for a single entity ODataWriter writer = messageWriter.CreateODataEntryWriter(); // start the entry writer.WriteStart(new ODataEntry() { // the edit link is relative to the //baseUri set on the writer in the case EditLink = new Uri("/Customers('" + "1" + "')", UriKind.Relative), Id = "Customers('" + "1" + "')", TypeName = "NORTHWNDModel.Customer", Properties = new List<ODataProperty>(){ new ODataProperty(){ Name = "CustomerID", Value = "1" }, new ODataProperty(){ Name = "CompanyName", Value = "Company1" }, new ODataProperty(){ Name = "ContactName", Value = "Contact1" }, new ODataProperty(){ Name = "ContactTitle", Value = "Title1" } } }); //create a non-expanded link for the orders navigation property writer.WriteStart(new ODataNavigationLink() { IsCollection = true, Name = "Orders", Url = new Uri("http://microsoft.com/Customer(" + "1" + ")/Orders") }); writer.WriteEnd(); //ends the orders link //create a non-expanded link for the employees navigation property writer.WriteStart(new ODataNavigationLink() { IsCollection = true, Name = "Employees", Url = new Uri( "http://microsoft.com/Customer(" + "1" + ")/Employees") }); writer.WriteEnd(); //ends the Employees link writer.WriteEnd(); //tells the writer we are done writing the entity writer.Flush(); //always flush at the end ODataMessageReader orm = new ODataMessageReader(message); } return message; }

    As you can see I am building up a HTTPClientRequestMessage this part of the code seems to work as expected. I can add OData properties at Run time which is a requirement.

    The problem is I cannot return the data as I don't know how to convert the OData that has been written back to a HttpResponseMessage.

    From searching the web I can find very little information to assist.

    I want to return an Atom feed. I had tried to use SyndicationItem to return the Atom field. However even though this worked the format wasn't what I required.

    I need to be able to return <m:properties> notes on my call as the return is being consumed by PowerPivot.


    Friday, June 29, 2012 4:14 PM

All replies

  • Instead of going low level implementation i would suggest  to used WCF Data Service with Reflection Provider that will take much of the pain.
    Sunday, July 1, 2012 5:45 AM