locked
Running twice through IEnumerable returned by DataServiceContext.Execute does not work RRS feed

  • Question

  • I have a WCF DataService in an ASP.NET Web application and a .NET console application as a client consuming this service. With the following code on the client...


     

    static void Main(string [] args)
    {
        MyContext aCtx = new MyContext(new Uri("http://server/MyDataService.svc"));
    
        IEnumerable<Order> result = aCtx.Execute<Order>(new Uri("Order", UriKind.Relative));
    
        foreach (Order x in result)
        {
            int ID = x.OrderID;
        }
        foreach (Order x in result)
        {
            int ID = x.OrderID;
        }
    }
    
    
    
    ... I get a "System.NotSupportedException" when the second loop starts. The first loop runs fine. Similar in other situations when I try to fetch the results - for instance looking at the result list in the debugger works for the first time, but not for the second time. Or calling result.Count() twice in a row works only the first time.

    What could be the reason?

    Thanks in advance!

     

    • Edited by Slauma Monday, April 5, 2010 7:33 PM correction
    Monday, April 5, 2010 7:32 PM

Answers

  • The IEnumberable returned by the client library doesnt support the Reset method as we stream the results down .

    The stream cannot be reset and hence you see the above error.

    Change your code to be :

    static void Main(string [] args)
    {
        MyContext aCtx = new MyContext(new Uri("http://server/MyDataService.svc"));

        IEnumerable<Order> result = aCtx.Execute<Order>(new Uri("Order", UriKind.Relative)).ToList();

        foreach (Order x in result)
        {
            int ID = x.OrderID;
        }
        foreach (Order x in result)
        {
            int ID = x.OrderID;
        }
    }


    This should work as it streams down the results and caches it into a List .


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Monday, April 5, 2010 8:44 PM
    Moderator

All replies

  • The IEnumberable returned by the client library doesnt support the Reset method as we stream the results down .

    The stream cannot be reset and hence you see the above error.

    Change your code to be :

    static void Main(string [] args)
    {
        MyContext aCtx = new MyContext(new Uri("http://server/MyDataService.svc"));

        IEnumerable<Order> result = aCtx.Execute<Order>(new Uri("Order", UriKind.Relative)).ToList();

        foreach (Order x in result)
        {
            int ID = x.OrderID;
        }
        foreach (Order x in result)
        {
            int ID = x.OrderID;
        }
    }


    This should work as it streams down the results and caches it into a List .


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Monday, April 5, 2010 8:44 PM
    Moderator
  • Great! It works with ToList() and ToArray() as well.

    Thank you very much!

    Monday, April 5, 2010 9:25 PM