Pergunta

I have some code that send a simple xml web request. It is called from a windows service. Sometimes the service starts throwing exceptions (System.Net.WebException: The operation has timed out) and a restart of the service fixes the issue. Here is the code:

    public bool PerformXmlRequest(string xml)
    {
        var httpRequest = (HttpWebRequest)WebRequest.Create(_url);

        httpRequest.Method = "POST";

        httpRequest.ContentType = "text/xml";

        using (var xmlWriter = new StreamWriter(httpRequest.GetRequestStream(), Encoding.UTF8))
        {
            xmlWriter.WriteLine(xml);
        }

        using (var httpResponse = (HttpWebResponse)httpRequest.GetResponse())
        {
            return httpResponse.StatusDescription == "OK";
        }
    }

Is there anything obviously wrong with it that might be causing this issue?

Foi útil?

Solução

There is nothing I can find is wrong with the calling code.

Is the error generated by client side code or does it come from the service?

If it is from the service it's the service that needs to be fixed, idéaly the service should never timeout no matter what you send it, it should fail in a more controlled maner giving a beter error message.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top