Dave Mateer
Dave Mateer

Reputation: 6626

The underlying connection was closed: An unexpected error occurred on a receive

Using a console app which runs as a scheduled task on Azure, to all a long(ish) running webpage on Azure.

Problem after a few minutes: The underlying connection was closed: An unexpected error occurred on a receive

 //// used as WebClient doesn't support timeout
public class ExtendedWebClient : WebClient
{
    public int Timeout { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        if (request != null)
            request.Timeout = Timeout;
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        return request;
    }

    public ExtendedWebClient()
    {
        Timeout = 1000000; // in ms.. the standard is 100,000
    }
}

class Program
{
    static void Main(string[] args)
    {
        var taskUrl = "http://www.secret.com/secret.aspx";
        // create a webclient and issue an HTTP get to our url
        using (ExtendedWebClient httpRequest = new ExtendedWebClient())
        {
            var output = httpRequest.DownloadString(taskUrl);
        }
    }
}

Upvotes: 1

Views: 6642

Answers (4)

flávio queiroz
flávio queiroz

Reputation: 51

I read a lot of tips to solve the problem, but nobody noted if there was a web proxy involved. If it was, please include in your code:

Dim proxyObject As WebProxy = New WebProxy('http://webproxy/, True)
Dim request As HttpWebRequest = WebRequest.Create(url)
request.Proxy = proxyObject

Moreover, there is a good article about possible solutions: here

Upvotes: 0

Richard Mitchell
Richard Mitchell

Reputation: 421

There is a good article on TechNet surrounding best practices for making robust connections to SQL Azure. The main take-away is that you have to build in some retry logic should the connection fail.

http://social.technet.microsoft.com/wiki/contents/articles/1541.aspx

Upvotes: 1

user94559
user94559

Reputation: 60143

Note that the Windows Azure load balancers terminate idle TCP connections after 60 seconds. It looks like you're calling through the load balancer... is there a chance the operation is taking longer than 60 seconds without sending anything down the wire?

Upvotes: 2

Peter Wone
Peter Wone

Reputation: 18765

Run something like Wireshark to watch the connection and find out which end is closing it. This sort of weirdness can be caused by a router between you and your server having an MTU lower than yours. I think you can set the MTU in WCF config, don't know about Azure.

Upvotes: 0

Related Questions