Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono Apache2 HttpWebRequest crashes with "The request timed out"

Tags:

c#

asp.net

mono

I am using a payment gateway API in my ASP.Net application. When testing in MonoDevelop with XSP the application works. When I configure it to run in apache2 with mod_mono the code keeps crashing with a timeout error.

I am stumped as to what could change with hosting in Apache instead of XSP. Anyways below is the code that is timing out:

private string SubmitXml(string InputXml)
{
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(_WebServiceUrl);
    webReq.Method = "POST";

    byte[] reqBytes;

    reqBytes = System.Text.Encoding.UTF8.GetBytes(InputXml);
    webReq.ContentType = "application/x-www-form-urlencoded";
    webReq.ContentLength = reqBytes.Length;
    webReq.Timeout = 5000;
    Stream requestStream = webReq.GetRequestStream();
    requestStream.Write(reqBytes, 0, reqBytes.Length);
    requestStream.Close();

    HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.ASCII))
    {
        return sr.ReadToEnd();
    }
}

The code is crashing on the line: Stream requestStream = webReq.GetRequestStream();

The error returned is:

The request timed out

Description: HTTP 500. Error processing request.

Stack Trace:

System.Net.WebException: The request timed out at System.Net.HttpWebRequest.GetRequestStream () [0x0005f] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System/System.Net/HttpWebRequest.cs:746 at TCShared.PxPay.SubmitXml (System.String InputXml) [0x00048] in /Users/liam/Projects/techcertain/techcertaincsharp/Components/TCShared/PaymentGateways/Client/PxPay.cs:85 at TCShared.PxPay.GenerateRequest (TCShared.RequestInput input) [0x00015] in /Users/liam/Projects/techcertain/techcertaincsharp/Components/TCShared/PaymentGateways/Client/PxPay.cs:69

In my Web.Config I have the following as the request timeout:

<httpRuntime executionTimeout="43200" maxRequestLength="104856" requestValidationMode="2.0"  />

I have tried changing the Timeout value on the HttpWebRequest but it still is timing out.

What is causing this to happen and how can I fix it?

like image 278
startupsmith Avatar asked Mar 31 '12 03:03

startupsmith


1 Answers

I managed to find out why I was experiencing this problem. It is completely unrelated to the use of Apache.

I am using Npgsql for database access to Postgresql. Npgsql comes with two dlls (Npgsql.dll and Mono.Security.dll). For some unknown reason Mono.Security.dll causes the HttpWebRequest to timeout when running on Mono.

Anyways Mono.Security.dll isn't needed when running on Mono because it is already included in the Mono framework. So after removing the Mono.Security dll from my bin directory HttpWebRequest's are now working.

Full credit goes to this post here http://mono.1490590.n4.nabble.com/The-request-timed-out-at-HttpWebRequest-EndGetResponse-td2218213.html .

like image 60
startupsmith Avatar answered Oct 13 '22 15:10

startupsmith