wcf-data-servicesurl-parameters

WCF Data service and url parameters


I created a Service service.svc adding an WCF Service item in my Visual Studio Project. It´s defined followings:

public class Service : DataService<MyContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.UseVerboseErrors = true;
        config.SetEntitySetAccessRule("Items", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }

    protected override void HandleException(HandleExceptionArgs args)
    {
        Email.SendErrorToDeveloper(args.Exception);
        base.HandleException(args);
    }
}

And the Context:

public class MyContext
{

    public IQueryable<Item> Items
    {
        get {
            return something;
        }
    }
}

As definition the service is consumed calling http://localhost:53407/Service.svc/Items.

I'm lookung if there is a way to add parameters to the url, in order to use them on server side in order to filter items. For example http://localhost:53407/Service.svc/Items?year=2019
Is it possible? How?

I found a this but I was looking for a solution where I can use the parameters in the MyContext class.


Solution

  • I found an easy solution:

    protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
        CurrentDataSource.YourCustomVariable = DoWhatYouNeet(args.OperationContext);
        base.OnStartProcessingRequest(args);
    }
    

    The WCF Service allows to intercept each call by overriding OnStartProcessingRequest which passes the request arguments including DataServiceOperationContext where we find basically everything.