4

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'.

Ramppy Dumppy
  • 2,667
  • 7
  • 27
  • 37
  • 1
    Possible duplicate of [Await operator can only be used within an Async method](http://stackoverflow.com/questions/11836325/await-operator-can-only-be-used-within-an-async-method) – TomDoesCode Jan 05 '16 at 13:11

2 Answers2

3
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;

        }
    }
}
Gopal SA
  • 949
  • 2
  • 17
  • 36
1

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();
    }