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 ?