Rui Machado

Technologies and Information Systems

  Home  |   Contact  |   Syndication    |   Login
  4 Posts | 3 Stories | 2 Comments | 0 Trackbacks

News

Twitter












Article Categories

Archives

Post Categories

Nowadays comunication between applications is an active topic with daily usage and a large amount of pratical appliances. While developing an app in witch I had to consume an OData I found out that combining Linq with my code made this operation pretty easy.

The algorithm to consume OData starts with adding a service reference to Visual Studio:

image

After adding the service reference in wich you define the uri to the service, we start building our code.

In your code the algorithm is the following:

  1. Define the Uri to your OData Service
  2. Define the context of your odata, wich contains all entities exposed by the service.
  3. Query the context using Linq
  4. Print the result

Easy and simple.

Example code:

01public static void Main(string[] args){
02 
03        Uri serviceUri= newUri("http://example.host.odataservice.net/service.svc", UriKind.Absolute);
04        ODataService.ServiceEntities context = newODataService.ServiceEntities (serviceUri);
05 
06        context.Credentials = newSystem.Net.NetworkCredential(Username,Password);
07 
08         var query = from ServiceObject in context.YourEntity
09                     select ServiceObject ;
10 
11        foreach (var myObject in query)
12        {
13            Console.WriteLine("\n Field1: {0} | Field2: {1}",
14            myObject .Field1, myObject .Field2);
15 
16        }
17}

That’s it.

Thank you,

Rui Machado

rpmachado.wordpress.com

posted on Sunday, October 7, 2012 8:49 PM

Feedback

# re: Consuming OData based Rest service in C# [en-US] 8/22/2013 9:42 PM Erik Beijer
Do you happen to know how to reuse a class library when adding a service reference to a REST service? The option doesn't appear to be present when adding REST services :(

Post A Comment
Title:
Name:
Email:
Comment:
Verification: