无法等待“方法组” IEnumerable

时间:2018-12-30 09:45:11

标签: c#

我遇到此错误,无法找到解决方案。基本上,此C#代码在Console项目中,该项目使用Web API方法来检索所有员工记录。

PrintOneEmployee方法有效,但PrintEmployee方法无效。

    static async Task PrintEmployee()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:63665/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //GET Method
            HttpResponseMessage response = await client.GetAsync("api/Employees");
            if (response.IsSuccessStatusCode)
            {
                IEnumerable<Employee> employee = await response.Content.ReadAsAsync<IEnumerable<Employee>();

                //for loop employee

            }
            else
            {
                Console.WriteLine("Internal server Error");
            }

        }
        Console.Read();
    }

    static async Task PrintOneEmployee()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:63665/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //GET Method
            HttpResponseMessage response = await client.GetAsync("api/Employees/5");
            if (response.IsSuccessStatusCode)
            {
                Employee employee = await response.Content.ReadAsAsync<Employee>();

                Console.WriteLine("Id:{0}\n", employee.EmployeeID);

            }
            else
            {
                Console.WriteLine("Internal server Error");
            }

        }
        Console.Read();
    }

1 个答案:

答案 0 :(得分:2)

在此行:

IEnumerable<Employee> employee = await response.Content.ReadAsAsync<IEnumerable<Employee>();

您在>的末尾缺少结束符ReadAsAsync<IEnumerable<Employee>>