Soap service throws "The response ended prematurely, with at least<X> additional bytes expected." (IOException)

Viewed 1405

i'm migrating code from asp.net to core 3.1. I'm having the following trouble with a Soap Service: When I call the service's method, I get an IOException with this message: "The response ended prematurely, with at least additional bytes expected." The number is the amount of bytes which couldn't be read from the response stream (the remaining bytes).

  • The response is compressed with gzip.
  • In the ASP.Net code, the Soap service method was a synced method, and the one in the Core is Async method.

Moreover, after creating the service from the .WSDL file, I needed to change the service's base URL inside the Reference.cs file from http to https because all the responses returned with a Redirected status. When I changed the Reference.cs file manually, I got errors, so I created a CustomBinding in order to inject the URL with the Https schema:

var customBinding = new BasicHttpsBinding
    {
        MaxReceivedMessageSize = 20000000,
        MaxBufferPoolSize = 20000000,
        AllowCookies = false,
        BypassProxyOnLocal = false,
        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
        TextEncoding = Encoding.UTF8
    };

customBinding.CreateBindingElements().AddRange(
    new HttpsTransportBindingElement
    {
        MaxReceivedMessageSize = 20000000,
        MaxBufferSize = 20000000,
    });

_service = new masterPricerPortTypeClient(customBinding, new EndpointAddress("https://..."));
_service.Endpoint.Binding = customBinding;

After it, I can see in a network tracer that this request was responded with a valid response, but when I'm trying to process the response, I get the IOException above.

I tried to call the action from the service client:

SearchReply searchReply = await _service.SearchAsync(searchRequest);

But I get the Exception in this line of the code above.

I tried to call the method via HttpWebRequest:

using (var resp = (HttpWebResponse)webRequest.GetResponse())
{
    using (var contentStream = resp.GetResponseStream())
    {
        contentStream.ReadTimeout = 1000;
        var bufferSize = 2048;
        var buffer = new byte[bufferSize];
        var result = new List<byte>();

        try
        {
            var readBytes = 0;
            while ((readBytes = contentStream.ReadAsync(buffer).Result) != 0)
            {
                for (int i = 0; i < readBytes; i++)
                {
                    result.Add(buffer[i]);
                }
            }

But I get the Exception inside the "while" loop after few read actions.

Did anyone had this issue or can assist please?

0 Answers
Related