zoukankan      html  css  js  c++  java
  • WebClient does not support concurrent I/O operations 错误的解决办法

    今天利用 WebClient 的 DownloadData、DownloadFileAsync 等方法跨域请求数据时,没有发现什么问题。可是过了半天,查看错误日志时,却大吃了一惊,发现好多 WebClient does not support concurrent I/O operations 的错误信息。仔细查看 错误信息的 堆栈后,把出错的方法和URL Copy在浏览器中执行,输入相应的参数,却发现是可以调用的,这个就很郁闷了,看来这个错误不是每次调用都出现,就更难排查了。

    最后,google 了半天,并看了许多资料,发现 只要错误信息含有 does not support concurrent I/O operations 信息的,都是由 一个实例同时在多个地方调用产生的,通俗易懂的说,就是并发引起的。下面摘录一句google到得解释: 


    I don't think you can use a single WebClient instance to execute several
    HTTP requests at the same time. Try to create a WebClient instance per request,
    that should work just fine.


    最后在贴出来一下自己的代码: 


    private static WebClient WC;

    static Test()
    {
        WC = new WebClient();
        WC.Headers["User-Agent"] = Config.Host + "|" + ServiceKey;
    }

    原来自己的 WebClient 是静态全局的,所以,并发使用 WC 来操作数据的时间,就报了上述错误

    解决办法:每个使用到 WebClient 的地方,都 new 一个 WebClient 实例,防止出现并发。同时 does not support concurrent I/O operations 错误的方法,一般也是由于 并发引起的,解决方法请参照上一句。

  • 相关阅读:
    危险系数
    快速幂模板
    回文数字
    Echart图表使用
    http请求头中Referer的含义和作用
    有关程序员的时间管理
    数据库报错 java.sql.SQLException: The user specified as a definer ('root'@'%') does not exist
    sql中字符串替换
    前端获取数据库的datetime(时间戳) 转化成日期展示出来
    MySQL版本升级
  • 原文地址:https://www.cnblogs.com/xunziji/p/1943092.html
Copyright © 2011-2022 走看看