DCSIMG
Consuming Data Services Service Operations - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Consuming Data Services Service Operations

Consuming Data Services Service Operations

In the following post Consuming Data Services Service Operations
I’m going to explain
how to consume
ADO.NET data services
service operations
from
your clients.

The Problem

In my previous post I introduced the concept of service operations
in ADO.NET data services. After adding a service reference in my client
application a problem raised it head. The service operations aren’t exposed
in the generated proxy. Probably this feature will be added in the future
by the ADO.NET team (I hope). Meanwhile, we need a solution to help us
consume the service operations (or else why should we use them).

Solving The Problem – Consuming Service Operations

The generated DataServiceContext holds the keys to solve the service operations
consuming problem. There are three methods that we can use in order to use
the service operation we built:

  • CreateQuery – which create a data service query.
  • Execute – which is used to query a data service by URI.
  • BeginExecute – which do the same as Execute but asynchronously.

CreateQuery Example

The following code demonstrate the use of CreateQuery to run the
service operation we built in the previous post:

// build the proxy

var proxy = new CoursesDataContext(

        new Uri("http://localhost:4205/CoursesService.svc/"));

// create a query with a the credits argument

var courses = proxy.CreateQuery<Course>("CoursesByCredits")

    .AddQueryOption("credits", 3);

// write to console the results

foreach (var course in courses)

{

    Console.WriteLine(

        "Course name: {0}, Course duration: {1}, Course credit: {2}",

        course.Title,

        course.Days,

        course.Credits

    );

}

as you can see I create a query with the operation name and attach
to it the credits argument.

Execute Example

The following code demonstrate the use of the Execute method in order to
run the service operation:

// build the proxy

var proxy = new CoursesDataContext(

        new Uri("http://localhost:4205/CoursesService.svc/"));


// execute the service operation

var courses = proxy.Execute<Course>(

    new Uri(string.Format("{0}CoursesByCredits?credit={1}",
                          proxy.BaseUri, 3),

            UriKind.RelativeOrAbsolute));

// write to console the results

foreach (var course in courses)

{

    Console.WriteLine(

        "Course name: {0}, Course duration: {1}, Course credit: {2}",

        course.Title,

        course.Days,

        course.Credit

    );

}

In the use of the Execute method I need to supply the URI of the
operation. The method returns an IEnumerable collection.

BeginExecute Example

The following code demonstrate the use of the BeginExecute method in order
to run the service operation:

// build the proxy

var proxy = new CoursesDataContext(

        new Uri("http://localhost:4205/CoursesService.svc/"));

// execute the query asynchronously
proxy.BeginExecute<Course>(

          new Uri(string.Format("{0}CoursesByCredits?credit={1}",
                                proxy.BaseUri, 3),

                  UriKind.RelativeOrAbsolute),

          OnQueryComplete, null);

The case of BeginExecute method is very similar to the Execute method
but we need to supply a callback function which in my example is called
OnQueryComplete:

private void OnQueryComplete(IAsyncResult result)

{

    IQueryable<Course> queryResults =

        proxy.EndExecute<Course>(result).AsQueryable();

    foreach (var course in queryResults)

    {

        Console.WriteLine(

            "Course name: {0}, Course duration: {1}, Course credit: {2}",

            course.Title,

            course.Days,

            course.Credit

            );

    }

}

Summary

Lets sum up, I showed how to consume a service operation from
a .Net client. I really hope that the ADO.NET team will expose the
service operations from the generated proxy when we add a service
reference to our applications. If they wont do that I gave you three ways
to consume service operations in the post.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 14, 2008 10:13 AM

Data Access Technologies said:

There are scenarios where we may want to add additional methods to an Entity class. When we implement

# December 14, 2010 3:17 PM

Data Access Technologies said:

There are scenarios where we may want to add additional methods to an Entity class. When we implement

# January 9, 2012 8:59 PM