我正在尝试列出搜索各种网络服务的航班列表,并返回异常航班。我创建了一个任务,希望有人验证并给我提示改进。
任务等待最多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
}
responseAvail
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;
}
}
Sabre Broker
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;
}
}
Tam Broker假货
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;
}
}
答案 0 :(得分:0)
这不一定能正常工作。
现在,您正在使用List<T>
来存储结果,但是通过Task.Factory.StartNew
使用ThreadPool来生成它们。这不是线程安全的,如果结果同时返回,可能会导致问题。
话虽这么说,一般来说,这段代码将受益于使用新的async
和await
语言功能,并将操作作为异步操作运行。这样可以避免完全使用线程池,从而提供更高的可伸缩性。