我正在使用将调用OData Service的应用程序。我尝试过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' await'运算符只能在异步方法中使用。考虑使用' async'标记此方法。修饰符并将其返回类型更改为'任务'。
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:0)
这更简单。我们需要SimpleQuery方法,因为我们无法将async关键字添加到Main或任何入口点方法。
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();
}