我正在尝试列出在各种网络服务中搜索并返回异常航班的航班列表。我创建了一个 TASK 并希望有人验证并给我改进的提示。
任务最多等待 30 秒。如果某些 Web 服务出现错误,我想返回,即使它给出了错误。
请查看代码并评估:
返回航班的任务
public List<datatypes.ResponseAvail> SearchRQWithTask(datatypes.RequestAvail rq)
{
List<datatypes.ResponseAvail> rsLst = new List<datatypes.ResponseAvail>();
try
{
var tasks = new List<Task>();
foreach (var broker in rq.brokers)
{
switch (broker.NomeBroker)
{
case Brokers.Azul:
brokers.Azul az = new brokers.Azul();
tasks.Add(Task.Factory.StartNew(() => rsLst.Add(az.SearchAzul(rq))));
break;
case Brokers.Sabre:
brokers.Sabre sabre = new brokers.Sabre();
tasks.Add(Task.Factory.StartNew(() => rsLst.Add(sabre.SearchSabre(rq))));
break;
case Brokers.TAM:
brokers.TAM tam = new brokers.TAM();
tasks.Add(Task.Factory.StartNew(() => rsLst.Add(tam.SearchTAM(rq))));
break;
default:
break;
}
}
Task.WaitAll(tasks.ToArray(), 10000);
}
catch (Exception ex)
{
rsLst[0].Error = ex;
}
return rsLst;
}
请求可用性
public class RequestAvail
{
public int idClienteRq { get; set; }
public List<BrokersList> brokers { get; set; }
public string origem { get; set; }
public string destino { get; set; }
}
public class BrokersList
{
public int id { get; set; }
public Brokers NomeBroker { get; set; }
}
public enum Brokers
{
Azul = 1,
Sabre = 2,
GOL = 3,
TAM = 4
}
响应有效
public class ResponseAvail : Exception
{
public int code { get; set; }
public string brokerName { get; set; }
public List<ListVoos> voos { get; set; }
public Exception Error { get; set; }
}
public class ListVoos
{
public int id { get; set; }
public string ori { get; set; }
public string des { get; set; }
public DateTime departure { get; set; }
public DateTime arrival { get; set; }
}
我从 Web 服务调用的代理。这是一个虚假的回报。
class Azul
{
public datatypes.ResponseAvail SearchAzul(datatypes.RequestAvail rq)
{
datatypes.ResponseAvail rs = new datatypes.ResponseAvail();
rs.brokerName = "AZUL";
rs.code = 100;
rs.voos = new List<datatypes.ListVoos>();
rs.voos.Add(new datatypes.ListVoos{
arrival = new DateTime(2013,05,10),
departure = new DateTime(2013,05,10),
id = 1,
ori="SAO",
des="GIG"
});
return rs;
}
}
军刀经纪人
public class Sabre
{
public datatypes.ResponseAvail SearchSabre(datatypes.RequestAvail rq)
{
datatypes.ResponseAvail rs = new datatypes.ResponseAvail();
try
{
rs.brokerName = "Sabre";
rs.code = 5665;
rs.voos = new List<datatypes.ListVoos>();
rs.voos.Add(new datatypes.ListVoos
{
arrival = new DateTime(2013, 05, 10),
departure = new DateTime(2013, 05, 10),
id = 1,
ori = "SAO",
des = "GIG"
});
Thread.Sleep(100);
//
//if (rs.voos[0].ori == "SAO")
//{
// throw new Exception("Erro no source Sabre....");
//}
}
catch (Exception ex)
{
throw ex;
}
return rs;
}
}
谭经纪人假的
public class TAM
{
public datatypes.ResponseAvail SearchTAM(datatypes.RequestAvail rq)
{
datatypes.ResponseAvail rs = new datatypes.ResponseAvail();
rs.brokerName = "TAM";
rs.code = 300;
rs.voos = new List<datatypes.ListVoos>();
rs.voos.Add(new datatypes.ListVoos
{
arrival = new DateTime(2013, 05, 10),
departure = new DateTime(2013, 05, 10),
id = 1,
ori = "SAO",
des = "GIG"
});
return rs;
}
}