0

The class ODataMessageWriter in Microsoft.Data.OData.dll, v5.6.1.0 accepts constructors that take IODataRequestMessage requestMessage as well as IODataResponseMessage responseMessage. The question is when to use one or the other.

I'm implementing a OData service by hand using ODataMessageWriter, and I'd like to get the headers of my response correct based on the headers in the request... but there seems to require a lot of manual coding to get all the headers right (accept, content-type, DataServiceVersion, etc)... Am I missing something?

Nestor
  • 13,706
  • 11
  • 78
  • 119

1 Answers1

1

If the HTTP message you're writing is a response message (i.e., from the server), then you'll use IODataResponseMessage. If you're constructing a message from a client, you'll use IODataRequestMessage. It sounds like you're writing a server, so you should be using IODataResponseMessage when creating writers and IODataRequestMessage when creating readers.

You're right that there's a lot of work involved when using ODataLib directly. ODataLib is great when you want/need to write your own server and need a component that knows how to serialize the OData payload format. If you don't need such a high degree of control over your server, I'd recommend using ASP.Net Web API's OData implementation, which actually uses ODataLib under the hood.

Having said that, ODataLib can figure the Content-Type to respond with if you give it the Accept header from the request. You just need to call SetContentType on the writer settings:

var settings = new ODataMessageWriterSettings();
settings.SetContentType(
  "application/json;q=.4, text/html", // Accept
  "iso-8859-5, unicode-1-1;q=0.8"     // Accept-Charset
);
Jen S
  • 4,465
  • 1
  • 32
  • 28
  • Jen...I was hoping to catch your answer here. I also left you a comment in your great blog. Follow up question: how about dealing with dataserviceversion, min max etc? – Nestor Apr 09 '14 at 00:28
  • I saw your comment over there too, but this is probably better for hitting a wider audience anyway :) As far as DataServiceVersion, I don't have a simple answer for you. You could keep it relatively simple and always choose to return the highest version you support in DataServiceVersion. It gets compilcated when the client requests a MaxDataServiceVersion lower than the one you support. One way to keep it easy for yourself would be to return an error anytime the user requests a MaxDataServiceVersion lower than what you support (just one idea). – Jen S Apr 09 '14 at 17:40
  • (Sorry, this is a very brief response -- when I get a chance, I might come back and comment more in detail, but you might want to ask a separate stack overflow question on OData versioning to attract a wider audience) – Jen S Apr 09 '14 at 17:59