WCF returning Streams or Strings?

Easarcoidosis

New Member
I've been constantly getting more and more frustrated with WCF and the return types.I'm currently working on a method at the moment that reads data from SQL, creates new objects of a certain type based on the amount of data retrieved, and then assigns the object values with the data retrieved from SQL.I have functionality in place to serialize to JSON and XML.Ideally I would like to convert my serialized objects into strings so that I can read the response from a browser in the converted fashion, hopefully the following examples will be more explanatory.The benefits I get from when I serialize my objects, and return a stream is that the formatting is perfect, and it is using my DataMembers in the classes, so a response would in JSON would look like:\[code\]{"GameID":1,"ProposalID":5}\[/code\]however when I go to retrieve the data in the browser, it asks me to save the stream file, I open the stream in notepad to see this result. This isn't ideal as the result isn't coming up on the browser as a string.If I return a string, I get the data, however it is not formatted properly using the DataMembers or any of the advantages from serializing and the information is therefore pretty bare and not meaningful:\[code\]"3151"\[/code\]As you can see from this example, I am also retrieving 2 SQL fields of 2 variables each. And I am unsure as to how I can append results onto a stream as at the moment it is only returning the last result (5,1), let alone return them properly.Although I'm not sure if I can paste a url of something that is what I would like to achieve, here it is anyway: http://api.justin.tv/api/user/show/justin.jsonMy immediate thought would be to use an XMLWriter or some kind to manually write the information, however I would like to be able to pay dividends to the serialization and DataMember features.My return stream Code:\[code\]foreach (MatchProposal proposal in Proposals){ returnStream = WriteJSON(SerializeToJSON(proposal));}public MemoryStream SerializeToJSON(object serializeObject){ DataContractJsonSerializer serializer = new DataContractJsonSerializer(serializeObject.GetType()); MemoryStream memoryStream = new MemoryStream(); serializer.WriteObject(memoryStream, serializeObject); return memoryStream;}public Stream WriteJSON(MemoryStream memoryStream){ string json = Encoding.Default.GetString(memoryStream.ToArray()); return new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(json));}\[/code\]And my unfulfilled simple String returning code:\[code\] WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;\[/code\]It simply returns the SQL Data.I hope I've given enough information into what I've done so far and what I would like to achieve from the example given. In Short I would like to achieve what is written in the Justin.tv API, I would like to take advantage of the serialization, however when I browse to the return stream in a browser, I am greeted with a file download which I can open in notepad which is not ideal.Thanks for any advice if I appear to have gotten lost at some point, or any resolutions I can implement :)Regards, Ronald
 
Top