1

I have created a Windows Service project, and i try to call SOAP Endpoint using WebReference.

My calls are successfully received by server, server is processing response, and returning response correctly, however im not able to read response when server returns me some error code.

When everything goes smoothly (the server returns with status code 200), im getting my desired response, but when server returns a custom error (in my example error code=404) with some details in body in xml, its causing an exception.

When i do the same with SOAP UI, i can see code: 404 with response body:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ImportOrderResponse xmlns="http://tempuri.org/">
         <ImportOrderResult>Location/Client not found.</ImportOrderResult>
      </ImportOrderResponse>
   </soap:Body>
</soap:Envelope>

But when i do it from my Windows Service project, the call is raising an exception. I can see correct code: 404, but when i try to read from a response stream i get an exception saying:

`System.ObjectDisposedException: 'Cannot access closed stream.'

My code looks like:

Plate[] plates = new Plate[1];
Plate plate = new Plate();
plate.BranchCenterCode = 1315;

plate.PlateNumber = "423";
plate.Quantity = 2;
plate.RegType = 384;
plate.SizeType = 2;
plates[0] = plate;
try
{
    webService.ImportOrder(plates);
}
catch (WebException error)
{
    //TODO: handle disposing
    if (error.Status == WebExceptionStatus.ProtocolError)
    { 
        WebResponse response = error.Response;
        Stream stream = response.GetResponseStream();
        byte[] bytes = new byte[stream.Length + 10]; // im getting the ObjectDisposedException at this line
        int numBytesToRead = (int)stream.Length;
        int numBytesRead = 0;
        do
        {
            int n = stream.Read(bytes, numBytesRead, 10);
            numBytesRead += n;
            numBytesToRead -= n;
        } while (numBytesToRead > 0);
        var type = stream.GetType();
        var reader = new StreamReader(stream);
        var respons = reader.ReadToEnd();
        
    }
}

Any ideas how to read response correctly ?

Bartosz Olchowik
  • 1,129
  • 8
  • 22
  • Which dotnet? Which WCF library wersion if not old framwork? – Selvin Jul 21 '23 at 10:13
  • @Selvin my Windows Service Project has .NET Framework 4.8. – Bartosz Olchowik Jul 21 '23 at 10:22
  • @Selvin However i just opened tab to add web reference again, and its written there that ` Add a Web reference instead of Service Reference. This will generate code based on .NET Framework 2.0 Web Services Technology.` – Bartosz Olchowik Jul 21 '23 at 10:28
  • are you testing "SOAP UI" and service on the same machine ? maybe error details are only on local machine ... (fx `customErrors` of web.config or something similar) – Selvin Jul 21 '23 at 10:28
  • @Selvin im testing it on my machine, requests are coming in both ways, nothing is wrong in that matter. – Bartosz Olchowik Jul 21 '23 at 10:29
  • about dotnet version I just thought that there may be bug in new dotnet implementation of WCF client ... – Selvin Jul 21 '23 at 10:30

0 Answers0