0

I have executed a query in my web browser that returns 100 results per page. This result set has a <link rel="next" href="..."> at the end which properly directs me to the next page of results.

The following code however, using Microsoft's ODataLib, does not fill in the ODataFeed.NextPageLink correctly and I am trying to understand why.

The code below is a single method extracted from one of my classes, which just assigns a URI. My ODataHttpRequestMessage class is just a wrapper for HttpWebRequest and returns the response as the result for IODataRequestMessage.GetStream and assigns the Headers property of the same.

All other properties and links are handled correctly, but not the ODataFeed.NextPageLink.

What can I do to further troubleshoot this or resolve the issue?

public void Go()
{
    ODataHttpRequestMessage request = new ODataHttpRequestMessage(this.Url);

    int count = 0;
    using (ODataMessageReader reader = new ODataMessageReader(request))
    {
        ODataReader feedReader = reader.CreateODataFeedReader();

        while (feedReader.Read())
        {
            switch (feedReader.State)
            {
                case ODataReaderState.NavigationLinkEnd:
                    ODataNavigationLink link = feedReader.Item as ODataNavigationLink;
                    break;

                case ODataReaderState.EntryEnd:
                    ODataEntry entry = feedReader.Item as ODataEntry;
                    count++;
                    break;

                case ODataReaderState.FeedEnd:
                    Microsoft.Data.OData.ODataFeed feed = feedReader.Item as Microsoft.Data.OData.ODataFeed;
                    break;
            }
        }
    }
}

Here is the implementation of ODataHttpRequestMessage:

internal sealed class ODataHttpRequestMessage : IODataRequestMessageAsync
{
    private readonly Uri url;
    private readonly HttpWebRequest request;
    private HttpWebResponse response;

    public Uri Url
    {
        get
        {
            return this.url;
        }

        set
        {
            throw new NotSupportedException("The url may not be changed once the request has been created.");
        }
    }

    public IEnumerable<KeyValuePair<string, string>> Headers
    {
        get
        {
            return this.response.Headers.ToLookup();
        }
    }

    public string Method
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public ODataHttpRequestMessage(Uri url)
    {
        this.url = url;
        this.request = HttpWebRequest.CreateHttp(url);
        this.request.Credentials = CredentialCache.DefaultNetworkCredentials;

        this.response = this.request.GetResponse() as HttpWebResponse;
    }

    public string GetHeader(string headerName)
    {
        return this.response.Headers[headerName];
    }

    public void SetHeader(string headerName, string headerValue)
    {
        this.request.Headers[headerName] = headerValue;
    }

    public Stream GetStream()
    {
        Stream stream = response.GetResponseStream();
        return stream;
    }

    public Task<Stream> GetStreamAsync()
    {
        Stream stream = this.GetStream();
        return Task.FromResult<Stream>(stream);
    }
}

At the end of the payload I mentioned that there is a link to the next page. Here is a minimal payload that demonstrates this and also causes the problem:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://a.b.c.d.e.com/service.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://a.b.c.d.e.com/Service.svc/Something</id>
    <title type="text">Something</title>
    <updated>2013-11-05T15:55:25Z</updated>
    <link rel="self" title="Something" href="Something" />
    <entry>
        <id>http://a.b.c.d.e.com/service.svc/Something('801b0100-1cee-430c-9767-febc4a4ad1e6')</id>
        <category term="IdentityItem.Something" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="Something" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/MemberOf" type="application/atom+xml;type=feed" title="MemberOf" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/MemberOf" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/UserOwners" type="application/atom+xml;type=feed" title="UserOwners" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/UserOwners" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ServiceUserOwners" type="application/atom+xml;type=feed" title="ServiceUserOwners" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/ServiceUserOwners" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/MemberUsers" type="application/atom+xml;type=feed" title="MemberUsers" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/MemberUsers" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/MemberServiceUsers" type="application/atom+xml;type=feed" title="MemberServiceUsers" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/MemberServiceUsers" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/MemberTeams" type="application/atom+xml;type=feed" title="MemberTeams" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/MemberTeams" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Children" type="application/atom+xml;type=feed" title="Children" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/Children" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/BusinessRole" type="application/atom+xml;type=entry" title="BusinessRole" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/BusinessRole" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RoleAssignments" type="application/atom+xml;type=feed" title="RoleAssignments" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/RoleAssignments" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/SuggestedDelegateFor" type="application/atom+xml;type=feed" title="SuggestedDelegateFor" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/SuggestedDelegateFor" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EscalationSchedules" type="application/atom+xml;type=feed" title="EscalationSchedules" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/EscalationSchedules" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CommunicationMethods" type="application/atom+xml;type=feed" title="CommunicationMethods" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/CommunicationMethods" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/Parent" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/PrimaryForUsers" type="application/atom+xml;type=feed" title="PrimaryForUsers" href="Something('801b0100-1cee-430c-9767-febc4a4ad1e6')/PrimaryForUsers" />
        <title />
        <updated>2013-11-05T15:55:25Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:Id>801b0100-1cee-430c-9767-febc4a4ad1e6</d:Id>
                <d:DisplayName>A Great Something</d:DisplayName>
                <d:ParentId>53ec9c58-c882-4a0b-8119-15cc517eee30</d:ParentId>
                <d:ParentNodeType>Something</d:ParentNodeType>
                <d:Tag m:null="true" />
                <d:Name>The name of a something</d:Name>
                <d:Created m:type="Edm.DateTime">2013-03-13T06:14:12Z</d:Created>
                <d:Modified m:type="Edm.DateTime">2013-10-11T03:42:32Z</d:Modified>
                <d:Description m:null="true" />
                <d:Status>Active</d:Status>
                <d:LegacyId m:type="Edm.Int32">2136</d:LegacyId>
                <d:BoundaryNodeType m:null="true" />
                <d:BoundaryNode m:type="IdentityItem.ExternalReference" m:null="true" />
                <d:IsDelegated m:type="Edm.Boolean">false</d:IsDelegated>
                <d:IsAnyRoleAssigned m:type="Edm.Boolean">true</d:IsAnyRoleAssigned>
                <d:Alias>An alias of something</d:Alias>
                <d:Category>Custom</d:Category>
                <d:BoundaryNodeId m:null="true" />
                <d:RelativePath>A relative path to a something</d:RelativePath>
            </m:properties>
        </content>
    </entry>
    <link rel="next" href="http://a.b.c.d.e.com/Service.svc/Something?$top=999900&amp;$skiptoken='19579'" />
</feed>
3
  • I don't see anything obviously wrong here, but I'd be surprised if this was a bug in ODataLib since it's a pretty common scenario. You're sure you're checking at FeedEnd and not FeedStart? To debug this further, maybe you could try simplifying your test payload to make it small and repro-able, and post that payload here?
    – Jen S
    Nov 5, 2013 at 3:53
  • @JenS I went ahead and added the test payload in there, it's just one query that originally retrieved 100 records, but I took off the next 99, kept the link and feed end, and rewrote some of the property names to obscure them. Nov 5, 2013 at 16:07
  • Ok, thanks. I've added an answer below.
    – Jen S
    Nov 6, 2013 at 1:44

1 Answer 1

1

You're treating the stream as a request message instead of a response message. Since you're implementing a client, you will be reading responses and writing requests. Request payloads should not have an next link (since requests are sent from the client to the server, and a server would never follow a client-generated next link), so the ODataLib reader will ignore it when reading a request.

The ODataMessageReader constructor can take an implementation of either IODataResponseMessage or IODataRequestMessage as a parameter. This determines whether ODataLib reads the message using response or request reading rules.

Instead of this:

IODataRequestMessageAsync request = new ODataHttpRequestMessage(this.Url);

using (ODataMessageReader reader = new ODataMessageReader(request))
{
   ...
}

Your code should look similar to this:

IODataResponseMessageAsync response = new ODataHttpResponseMessage();

using (ODataMessageReader reader = new ODataMessageReader(response))
{
   ...
}
2
  • I was being hacky and fusing the functionality of the request and response. This makes sense now. I will make sure it works tomorrow morning and accept the solution if that is the case, which I think it will be. Nov 6, 2013 at 5:06
  • Something I would have done if I were the author of the library is throw an exception when you access that property, indicating that it is an invalid operation to try and get or set the NextPageLink when it is associated with a request. Nov 6, 2013 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.