当前位置: 动力学知识库 > 问答 > 编程问答 >

c# 4.0 - 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'.

网友答案:
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;

        }
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: