开发者

List<> items lost during ToArray()?

开发者 https://www.devze.com 2023-03-13 08:41 出处:网络
A while ago, I coded a system for collecting public transport disruptions. Information about any incident is collected in an MSSQL database. Consumers access these data by calling an .asmx web service

A while ago, I coded a system for collecting public transport disruptions. Information about any incident is collected in an MSSQL database. Consumers access these data by calling an .asmx web service. Data are collected from the DB using ADO.NET, each data row is then populating a Deviation object and added to a List. In the service layer, the list is applied a ToArray() call and returned to the consumer.

So far, so good. But the problem is that in some cases (5% or so), we have been aware that the array somehow is curtailed. Instead of the usual number of 15-20 items, only half of them, or even fewer, are returned. The remaining items are always at the end of the original list. And, even fewer times, a couple of items are repeated/shuffled at the beginning of the array.

After doing some testing on the different layers, it seems as the curtailing occurs at the end of the process, i.e. during the casting to an array or the SOAP serialization. But the code seems so innocent, huh??:

[WebMethod]
public Deviation[] GetDeviationsByTimeInterval(DateTime from, DateTime to)
{
    return DeviRoutines.GetDeviationsByTimeInterval(from, to).ToArray();
}

I am not 100% sure the error doesn't occur in the SQL or data access layer, but they have proved to do their job durin开发者_如何学JAVAg the testing. Any help on the subject would be of great help! :)


I'd do something like:

public Deviation[] GetDeviationsByTimeInterval(DateTime from, DateTime to)
{
    var v1 = DeviRoutines.GetDeviationsByTimeInterval(from, to);
    LogMe( "v1: " + v1.Count );

    var v2 = v1.ToArray();
    LogMe( "v2: " + v2.Length );

    return v2;
}

Proofing what you expect usually pays out :-)


http://msdn.microsoft.com/en-us/library/x303t819.aspx

Type: T[] An array containing copies of the elements of the List.

You didn't find a bug in .NET, it's most likely something in your GetDeviationsByTimeInterval


I'd be willing to bet ToArray is doing exactly what it's told, but either your from or to values are occasionally junk (validation error?) or GetDeviationsByTimeInterval is misinterpreting them for some reason.

Stick some logging into both Deviation[] and GetDeviationsByTimeInterval to see what values get passed to it, and the next time it goes pear-shaped you'll be able to diagnose where the problem is.

0

精彩评论

暂无评论...
验证码 换一张
取 消