Ramppy Dumppy
Ramppy Dumppy

Reputation: 2817

Simple.OData.Client in .Net

I am working with an application that will call OData Service. I tried the Simple.OData.Client but I can't get it working..

Here is the code that I try

var client = new ODataClient("http://packages.nuget.org/v1/FeedService.svc/");
var packages = await client.FindEntriesAsync("Packages?$filter=Title eq 'Simple.OData.Client'");
foreach (var package in packages)
{
    Console.WriteLine(package["Title"]);
 }

I get this error

Error 1 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

Upvotes: 4

Views: 4652

Answers (2)

Miklos Nemeth
Miklos Nemeth

Reputation: 21

This is even simpler. We need SimpleQuery method, since we cannot add the async keyword to the Main or any entry point methods.

    static async void SimpleQuery()
    {
        var client = new ODataClient("http://blahb...lah.svc/");
        try
        {
            var packages = await client.FindEntriesAsync("Products");
            foreach (var package in packages)
            {
                Console.WriteLine(package);
            }
        } catch (Exception e)
        {
            Console.WriteLine("Simple Query " + e);
        }
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Press Enter when the job is completed");
        SimpleQuery();
        Console.ReadLine();
    }

Upvotes: 1

Gopal SA
Gopal SA

Reputation: 959

using System;
using Simple.OData.Client;

namespace ODataClient
{
    class Program
    {
        static void Main(string[] args)
        {   
            new Manager().GetData();    
            Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ODataClient
{
    public class Manager
    {
        private readonly Simple.OData.Client.ODataClient client;

        public Manager()
        {
            client = new Simple.OData.Client.ODataClient("http://packages.nuget.org/v1/FeedService.svc/");
        }

        public void GetData()
        {
            try
            {
                IEnumerable<IDictionary<string, object>> response = GetPackages().Result;

                foreach (var package in response)
                {
                    Console.WriteLine(package["Title"]);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private async Task<IEnumerable<IDictionary<string, object>>> GetPackages()
        {
            var packages = await client.FindEntriesAsync("Packages?$filter=Title eq 'Simple.OData.Client'");    
            return packages;

        }
    }
}

Upvotes: 3

Related Questions