SoapHttpClientProtocol:得到响应,流而不是字符串?字符串、而不是、SoapHttpClientProtocol

2023-09-05 01:51:53 作者:平生事

我使用的web服务而吐出大量数据的在一块。响应字符串可以像8MB。虽然不是在台式机上的问题,一个嵌入式设备去坚果处理一个8MB字符串对象。

我不知道是否有一种方式来获得响应为流?目前我使用类似下面的方法。我试图用POST请求代替,但SOAP仅仅是更方便(响应是XML和使用POST我有明文回复转换回有效的XML),我想坚持下去。是否有可能使用不同类型的调用,这将不会返回一个字符串,但流?有任何想法吗?

  [System.Web.Services.Protocols.SoapDocumentMethodAttribute(MyAPI /的MyMethod,RequestNamespace =MyAPI,ResponseNamespace =MyAPI,ParameterStyle = System.Web.Services.Protocols .SoapParameterStyle.Wrapped,使用= System.Web.Services.Description.SoapBindingUse.Literal)
    公共字符串的MyMethod(字符串SID)
    {
      [对象]结果= this.Invoke(的MyMethod,新的对象[] {SID});
      返回((字符串)(结果[0]));
    }
 

解决方案

如果您使用旧的ASMX Web服务客户端基础架构,那么你就坚持了它的局限性。一个限制是,没有简单的方式来获得,除了作为并行化数据的响应。

Python request获取接口响应时间elapsed

如果它是必要的,那么你可以使用一个部分类重写 GetWebResponse 方法返回自己的自定义 WebResponse类。后者反过来将覆盖 GetResponseStream 方法调用基版,消费流,然后返回一个包含一个空的web请求流(否则.NET会窒息,没有内容的流)。

您也可以尝试通过重写 GetReaderForMessage 方法。这是通过一个 SoapClientMessage 实例,它具有的 ,你可能能够使用属性。同样,你必须设置流的东西,Web服务的基础设施可以消耗。

更好的方法做,这是一个WCF客户端。 WCF拥有更强大且易于使用的可扩展性机制。

事实上,你甚至不需要延长WCF客户端。你可能只是能够将它配置没有这个缓冲问题的了。

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

解决方案

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.