底层连接已关闭:接收时发生意外错误

时间:2011-06-21 03:09:06

标签: c# asp.net iis azure

使用在Azure上作为计划任务运行的控制台应用程序,在Azure上运行所有长(ish)运行的网页。

几分钟后出现问题:基础连接已关闭:接收上发生意外错误

 //// 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);
        }
    }
}

4 个答案:

答案 0 :(得分:2)

请注意,Windows Azure负载平衡器会在60秒后终止空闲TCP连接。看起来你正在通过负载均衡器进行呼叫......操作是否有可能超过60秒而不发送任何信息?

答案 1 :(得分:1)

TechNet上有一篇很好的文章,介绍了与SQL Azure建立强大连接的最佳实践。主要的好处是,如果连接失败,你必须构建一些重试逻辑。

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

答案 2 :(得分:0)

运行类似Wireshark的东西来观察连接并找出哪一端正在关闭它。这种奇怪之处可能是由于您和服务器之间的路由器MTU低于您的路由器造成的。我想你可以在WCF配置中设置MTU,不了解Azure。

答案 3 :(得分:0)

我阅读了很多解决该问题的技巧,但没有人指出是否涉及Web代理。如果是这样,请在您的代码中添加:

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

此外,有一篇很好的文章介绍了可能的解决方案:here