An Avanade Blogging Community

Welcome to An Avanade Blogging Community Sign in | Join | Help
in Search

Johan's Avanade Blog

ADO.NET Data Services: how to invoke a WebGet service operation from a WebClient

I have been working lately with ADO.NET Data Services, and I found several tutorials on how to create your first services and service operations.  But, then once I wanted to consume my service operations (WebGet), I was in the clouds...

So, here are my 2 cents...

First, here is the context of the service operation on the server:

public class MyService : DataService<MyEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(IDataServiceConfiguration config)
        {
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        }

        [WebGet]
        public IQueryable<Program> GetPrograms(int code, DateTime startDate, DateTime endDate)
        {
            return from program in this.CurrentDataSource.Program
                   where program.Code == code
                      && program.Date >= startDate.Date
                      && program.Date < endDate.Date
                   select program;
        }

}

Then, on the client I can invoke it as follow:

public IList<Program> Retrieve(int code, DateTime startDate, DateTime endDate)
{           
        var context = new MyService.MyEntities(<service url>);
        context.MergeOption = MergeOption.AppendOnly;
        var list = context.CreateQuery<Program>("GetPrograms")
            .AddQueryOption("code", code)
            .AddQueryOption("startDate", string.Format("datetime'{0:yyyy-MM-ddTHH:mm:ss}'", startDate))
            .AddQueryOption("endDate", string.Format("datetime'{0:yyyy-MM-ddTHH:mm:ss}'", endDate));

        return list.ToList();
}

Note that the parameters must be added inline to the CreateQuery method, and I had to use specific DateTime format for my parameters.

Links

Create Data-Centric Web Applications With Silverlight 2

ADO.Net Data Services Part 1 - Building a Simple Web Data Service

ADO.Net Data Services Part 2 - Using Service Operations with WebGet

Service Operations in ADO.NET Data Services

Published Thursday, October 16, 2008 10:49 AM by johanr

Comments

No Comments
Anonymous comments are disabled

This Blog

Post Calendar

<October 2008>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Syndication