2016-01-05 41 views
4

我正在使用將調用OData服務的應用程序。我試過Simple.OData.Client但我不能得到它的工作...Net中的Simple.OData.Client

下面是代碼,我嘗試

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"]); 
} 

我得到這個錯誤

錯誤1「等待」運算符只能在異步方法中使用。考慮使用「異步」修飾符標記此方法,並將其返回類型更改爲「任務」。

+1

的[等待操作者只能一個異步方法中使用]可能的複製(http://stackoverflow.com/questions/11836325/await-operator-can-only-be-used-within-an-異步方法) – TomDoesCode

回答

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; 

     } 
    } 
}