Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle continuations in WCF-OData with a URL query?

Tags:

.net

wcf

odata

I am working with a WCF Data Service pointing to an OData endpoint. If I use a DataServiceQuery, I can manage the continuation without any trouble.

var collection = new DataServiceCollection<T>();
collection.LoadCompleted += (sender, e) =>
    {
        if (e.Error != null)
        {
            callback(null, e.Error);
            return;
        }

        var thisCollection = (DataServiceCollection<T>) sender;
        if (thisCollection.Continuation != null)
        {
            thisCollection.LoadNextPartialSetAsync();
        }
        else
        {
            var items = thisCollection.ToList();
            callback(items, e.Error);
        }
    };
collection.LoadAsync(query);

However, I don't see how you can do the same for a DataServiceContext.BeginExecute(string url, ...) method.

_odataContext.BeginExecute<T>(new Uri(requestUrl), x =>
{
    var items = _odataContext.EndExecute<T>(x);

    //not sure how to get the rest of the items with this method
});

How can I use the url-based query method but still get continuation support?

like image 473
EndangeredMassa Avatar asked Apr 01 '11 15:04

EndangeredMassa


2 Answers

A synchronous sample (to make it simpler):

var r = ctx.Execute<Product>(new Uri("http://services.odata.org/Northwind/Northwind.svc/Products"));
QueryOperationResponse<Product> response = (QueryOperationResponse<Product>)r;
response.Count();
Console.WriteLine(response.GetContinuation());

In short, the Execute method returns instance of QueryOperationResponse, which implements IEnumerable but also exposes the continuation.

like image 61
Vitek Karas MSFT Avatar answered Sep 23 '22 02:09

Vitek Karas MSFT


For completeness, here's the full function that follows continuations for URL queries.

public void ExecuteFullQuery<T>(Uri requestUrl, Action<IEnumerable<T>> callback)
{
    var list = new List<T>();
    ExecuteFullQueryImpl(requestUrl, list, callback);
}

private void ExecuteFullQueryImpl<T>(Uri requestUrl, List<T> items, Action<IEnumerable<T>> callback)
{
    _odataContext.BeginExecute<T>(requestUrl, x =>
    {
        var results = _odataContext.EndExecute<T>(x);
        if (results != null)
            items.AddRange(results.ToList());

        var response = (QueryOperationResponse<T>)results;
        var continuation = response.GetContinuation();
        if (continuation != null)
        {
            ExecuteFullQueryImpl(continuation.NextLinkUri, items, callback);
        }
        else
        {
            callback(items);
        }
    },
    null);
}
like image 21
EndangeredMassa Avatar answered Sep 24 '22 02:09

EndangeredMassa