WCF返回流还是字符串? [英] WCF returning Streams or Strings?

查看:123
本文介绍了WCF返回流还是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对WCF和返回类型越来越感到沮丧. 我目前正在研究一种方法,该方法可从SQL读取数据,根据检索到的数据量创建特定类型的新对象,然后为对象值分配从SQL检索到的数据.

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.

我有适当的功能可以序列化为JSON和XML. 理想情况下,我想将序列化的对象转换为字符串,以便可以以转换后的方式从浏览器读取响应,希望以下示例可以提供更多说明.

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.

当我序列化对象并返回流时,我得到的好处是格式化是完美的,并且它在类中使用了我的DataMembers,因此在JSON中的响应如下所示:

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:

{"GameID":1,"ProposalID":5}

但是,当我去浏览器中检索数据时,它要求我保存流文件,我在记事本中打开流以查看此结果.这是不理想的,因为结果不会以字符串的形式出现在浏览器中.

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.

如果我返回一个字符串,则会得到数据,但是使用DataMembers不能正确格式化该数据,也不能通过序列化获得任何好处,因此该信息几乎是毫无意义的:

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:

"3151"

从该示例中可以看到,我还检索了每个包含2个变量的2个SQL字段.而且我不确定如何将结果附加到流上,因为它仅返回最后一个结果(5,1),更不用说正确地返回它们了.

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.

尽管我不确定是否可以粘贴想要实现的功能的URL,但无论如何,这里是:

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.json

我的直接想法是使用XMLWriter或某种方式手动编写信息,但是我希望能够从序列化和DataMember功能中受益.

My 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.

我的返回流代码:

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));
}

还有我未完成的简单String返回代码:

And my unfulfilled simple String returning code:

   WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;

它只是返回SQL数据.

It simply returns the SQL Data.

我希望我已经提供了足够的信息来说明到目前为止的工作以及我希望从给出的示例中实现的目标. 简而言之,我想实现Justin.tv API中编写的内容,我想利用序列化的优势,但是当我在浏览器中浏览到返回流时,我迎来了可以打开的文件下载在记事本中是不理想的.

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 :)

关于, 罗纳德(Ronald)

Regards, Ronald

推荐答案

您是否将ContentType应用于响应,例如text/xmltext/json?

Are you applying a ContentType to the response, e.g. text/xml, or text/json?

我想这可能是问题所在.因为您返回的是Stream而不是对象,所以我认为未指定任何内容类型,因此浏览器不知道如何处理它-这会提示下载消息.尝试添加text/json的内容类型以查看是否可以解决问题.然后,我将检查所请求的内容类型,并让您的服务适应该要求.

I would imagine this is likely to be the problem. Because you are returning a Stream instead of an object, I think that no content type is being specified, so the browser doesn't know how to handle it - this prompts a download message. Try adding in a content type of text/json to see if that sorts the issue. I would then check what content type is being requested, and have your service adapt to match that.

更新,我建议不要尝试将其简单地附加到流的末尾.相反,为什么不构建一个复合模型,该模型包含两个属性,这些属性表示要序列化并从服务传递回来的对象?

Update in regards to add a second object to the stream, I would recommend against trying to simply tack it on to the end of the stream. Instead, why not build a composite model, which contains two properties representing the objects to want to serialise and pass back from the service?

这篇关于WCF返回流还是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆