c# - Mixing XML and JSON in RESTful WCF without separate methods -


I have a secure WCF service, which can return XML, JSON or JSONP based on arguments, e.g. /service.svc/stuff?format=xml or service.svc / stuff? Format = json & callback = my callback . To do this, I have created a custom behavior, method encoder, and method encoder that prepares JSONP callback and selects the author based on format logic. In the WriteMessage () method of my encoder, I do something like this

  XmlWriter author = IsXmlRequested ()? XmlDictionaryWriter.CreateTextWriter (stream): JsonReaderWriterFactory.CreateJsonWriter (stream) message. Write message (author); After that, I define my service methods as if they just return to JSON but use my custom binding element:  

.Json, UriTemplate = "stuff") Public listing & lt; Thing & gt; GetStuff () {...}

And it works almost exactly when I ask for XML or JSON, I get something in the right format, but I hope that XML It has not been serialed here as to how XML looks:

  & lt; Root type = "array" & gt; & Lt; Item type = "object" & gt; & Lt; FirstPropertyOnAThing & gt; 1 & lt; / FirstPropertyOnAThing & gt;  

While I just wanted to set the WebMessageFormat to XML, I would get something like this:

  & lt; ArrayOfThings xmlns = "... & gt;; & lt; Thing ... & gt; FirstPostingIntegration & gt; 1 & lt; / Firstproperty Hosting & gt;  

I think that this is happening because the result is sorted into a dictionary message object has been created, my custom encoder is only deciding how to write that dictionary in the response stream So this encoding will be correct. Yes, but not exactly the format, which has already been decided by ResponseFormat.

First of all, is this right? If so, how can I fix it? For example Can I write my own webmass format? Or do I have to write different methods (and URI templates) and write which are the different attributes of the response format for JSS / * and / xml / *?

Update: In .NET 4, a WebOperationContext.Current.Outg OingResponse.Format is the property that you can set I think my issue boils down: is there a way to complete it in 3.5?

Yes, without writing a different method, which you want to do in .NET 3.5 , The way to complete

The situation you describe is related to: Replacing the content-type of feedback based on incoming requests. But, the post describes a slightly different approach to the solution.

Specifically, the requester does not specify the desired content type in the request URL, rather the request in the Accept header.

The solution uses custom, which inspects the acknowledged header and properly formatted the response. In my opinion, to get adaptive formatting, you have to do nothing in your business logic just attach the behavior and it works .


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -