从多个配置服务器列表中尝试连接 其实比较简单,主要有两个比较重要的变量,一个是服务器的索引,一个是尝试连接次数,当然,这些都是可以自己自由设置的。 下面是我写的一段列程,是我以前一个项目里涉及到的,我精简了一下,和大家探讨研究 public bool Conn( ref McServices.Services ms) { //ConnCount 尝试连接次数 //ServerIndex 配置的服务器索引 try { System.Threading.Thread.Sleep(3000);//堵塞3秒 ms = new McServices.Services(); ms.Url = ConfigurationSettings.AppSettings["Server"+ServerIndex]; if(ms.CheckServices()) { ConnCount = 0; ServerIndex = 1; Console.WriteLine(ms.Url + "连接成功!"); return true; } } catch(Exception e)//出现连接失败,则抛出异常 { if(ConnCount>=3) { ConnCount=0; if(ServerIndex>3) { Console.WriteLine("所有服务器尝试连接失败!"); return false; } else {ServerIndex ++;} return Conn(ref ms); } else { Console.WriteLine( ms.Url + "尝试连接失败" + (ConnCount+1) + "次,等待3秒以后尝试连接!"); ConnCount ++; return Conn(ref ms); } } return false; } 当然,其实还可以加入后台线程,作为检测是否吊线啊,正在连接啊,连接成功之类的检测功能,这些就大家自由发挥了 转载于:https://www.cnblogs.com/KeithDan/archive/2006/07/18/454167.html