SoapHttpClientProtocol:以流而不是字符串的形式获取响应?

发布于 2024-10-17 08:30:44 字数 683 浏览 4 评论 0原文

我正在使用一种网络服务,它可以一次性输出大量数据。响应字符串可能约为 8MB。虽然在台式电脑上这不是问题,但嵌入式设备在处理 8MB 字符串对象时会发疯。

我想知道是否有办法以流的形式获取响应?目前我正在使用如下方法。我尝试使用 POST 请求,但 SOAP 更方便(响应是 XML,对于 POST,我必须将纯文本回复转换回有效的 XML),我想坚持使用它。是否可以使用不同类型的“Invoke”,它不会返回字符串而是流?有什么想法吗?

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("MyAPI/MyMethod", RequestNamespace="MyAPI", ResponseNamespace="MyAPI", ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped, Use=System.Web.Services.Description.SoapBindingUse.Literal)]
    public string MyMethod(string sID)
    {
      object[] results = this.Invoke("MyMethod", new object[] { sID });
      return ((string)(results[0]));
    }

I'm using a webservice which spits out very large amounts of data in one piece. The response string can be something like 8MB. While not an issue on a desktop PC, an embedded device goes nuts dealing with an 8MB string object.

I wonder if there is a way to get the response as a stream? Currently I'm using the method like below. I tried using a POST request instead, but SOAP is just more convenient (the response is XML and with the POST I have to convert the plain text reply back to valid XML) and I'd like to stick with it. Is it possible to use a different kind of "Invoke" which won't return strings but streams? Any ideas?

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("MyAPI/MyMethod", RequestNamespace="MyAPI", ResponseNamespace="MyAPI", ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped, Use=System.Web.Services.Description.SoapBindingUse.Literal)]
    public string MyMethod(string sID)
    {
      object[] results = this.Invoke("MyMethod", new object[] { sID });
      return ((string)(results[0]));
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我三岁 2024-10-24 08:30:44

如果您使用旧的 ASMX Web 服务客户端基础设施,那么您就会受到其限制。一个限制是,除了反序列化数据之外,没有简单的方法来获取响应。

如果有必要,您可以使用分部类来覆盖 GetWebResponse 方法返回您自己的自定义 WebResponse。后者将依次覆盖 GetResponseStream方法调用基本版本,使用流,然后返回包含“空”Web 请求的流(否则 .NET 将因没有内容的流而阻塞)。

您也可以通过覆盖 来尝试类似的操作GetReaderForMessage 方法。这是通过 SoapClientMessage 具有 的实例您可能能够使用的 Stream 属性。同样,您必须将流设置为 Web 服务基础设施可以使用的内容。

更好的方法是使用 WCF 客户端。 WCF 具有更强大且易于使用的扩展机制。

事实上,您甚至可能不需要扩展 WCF 客户端。您可能只需将其配置为根本不存在此缓冲问题。

If you use the old ASMX web service client infrastructure, then you're stuck with its limitations. One limitation is that there's no simple way to get the response except as deserialized data.

If it were necessary, then you could use a partial class to override the GetWebResponse method to return your own custom WebResponse. This latter would in turn override the GetResponseStream method to call the base version, consume the stream, then to return a stream containing an "empty" web request (otherwise .NET will choke on a stream with no contents).

You might also try something similar by overriding the GetReaderForMessage method. This is passed a SoapClientMessage instance which has a Stream property that you might be able to use. Again, you'll have to set the stream to something that the web service infrastructure can consume.

The better way to do this is with a WCF client. WCF has much more powerful and easy to use extensibility mechanisms.

In fact, you might not even need to extend a WCF client. You might simply be able to configure it to not have this buffering problem at all.

苦笑流年记忆 2024-10-24 08:30:44

任何 Web 服务调用都会返回 SOAP,不是吗?我不认为流可以序列化为肥皂包以从您的服务返回。即使可以,序列化流是否至少与字符串本身一样大?

Any web service call is going to return SOAP, isn't it? I don't think a stream could be serialized into a soap packet to be returned from your service. And even if it could, wouldn't the serialized stream be at least as big as the string itself?

手心的温暖 2024-10-24 08:30:44

我相信答案是否定的,SOAP 中没有流的概念。

也许最简单的答案是采用您的方法:

  • 将您的响应解析为您的移动设备可以处理的段
  • 将您的响应缓存在应用程序变量中,因为这些段的字典
  • 返回 GUID 的数组列表。

然后,您可以让客户端通过 GUID 分别请求每个片段,然后在所有 Web 服务返回时重新组装原始响应并处理它。

I believe the answer is no, there is no concept of a stream for SOAP.

Probably the simplest answer is to have your method:

  • Parse your response into segments your mobile device can handle
  • Cache your response in a application variable as a dictionary of these segments
  • return an arraylist of GUIDs.

You can then have your client request each of these segments separately via their GUIDs, then reassemble the original response when and handle it all the web services return.

终止放荡 2024-10-24 08:30:44

ASMX 对此无能为力。 WCF 的 BasicHttpBinding可以将 Stream 返回给调用者。

http://msdn.microsoft.com/en-us/library/ms733742.aspx

ASMX can't do much about this. WCF's BasicHttpBinding can return a Stream to the caller.

http://msdn.microsoft.com/en-us/library/ms733742.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文