WCF Data Services Error: Resource not found for the segment 'xyz'.

by Larry Spencer Tuesday, May 29, 2012 8:47 AM

While exploring WCF Data Services (formerly called ADO.NET Data Services), I got an error that puzzled me for a while:

Resource not found for the segment 'xyz'.

Many causes for this error have been cited but I managed to create it in an apparently rare way (rare stupidity?). I thought I'd post it here on the off chance that it is puzzling someone as it puzzled me.

My Data Service had just two IQueryables: Records and its one-to-many child, Pages. As a first step, I got Records working so my DataService-derived class looked like this:

 

public class Repository : DataService<RepositoryEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Records", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

 

Everything worked great, so I added code to support Pages. Unfortunately, I forgot to call SetEntityAccessRule for it. I got data back from this URL...

http://localhost:54481/Repository.svc/Records

...but this one...

http://localhost:54481/Repository.svc/Records(1)/Pages

...gave me the error, 'Resource not found for the segment 'Pages'. The error came back as a nicely formatted XML page in Chrome, but a fairly opaque HTTP 404 Not Found error in Internet Explorer.

The remedy was to set the entity access rule for Pages as well:

 

public static void InitializeService(DataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("Records", EntitySetRights.AllRead);
    config.SetEntitySetAccessRule("Pages", EntitySetRights.AllRead);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}

 

Getting lazy with a "*" wildcard works, too:

 

public static void InitializeService(DataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}

Tags: , ,

All | WCF Data Services

Add comment

About the Author

Larry Spencer

Larry Spencer develops software with the Microsoft .NET Framework for ScerIS, a document-management company in Sudbury, MA.