Search code examples
c#wcfdynamic-data

Consume WCF Data Service resource dynamically


I have a WCF Data Service that exposes an Entity Framework database context and EntitySet like so,

public class DatabaseService : DataService<DatabaseContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {

        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:

        config.SetEntitySetAccessRule("People", EntitySetRights.AllRead);

        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

Is it possible to consume this service in a way that allows me to construct queries dynamically against the service using the full URI?

Ie. the URI I would be using is,

http://localhost:1353/DatabaseService.svc/People?$filter (my filter here)

I dont want any of my querying to be hard-coded, just dynamically invoked by changing the string-based URI.

Pretty sure its possible, but is the implementation simple or does it require alot of code, like a full dynamic proxy for the WCF service?

Ive only done stuff like this with plain SOAP services before.

EDIT

More specifically I want to be able to do something effectively like this,

IEnumerable<People> peopleResults = new DataServiceQuery("http://localhost:1353/DatabaseService.svc/People?$filter (my filter here)")

Where the results are populated right into my list without the client having to know anything except the full URI (including filters and all).


Solution

  • The DataServiceQuery class, once constructed for a type, has a RequestUri that you can set to customize the query. I don't know that it's the canonical usage but it would certainly work.