API提供了错误{{System.Runtime.CompilerServices.AsyncTaskMethodBuilder1 + AsyncStateMachineBox1}}

时间:2019-10-15 16:10:02

标签: c# .net api .net-core

我正在创建一个API使用工具,该工具存在一个问题,当我尝试调用API时出现以下错误。请帮我解决一下这个。我正在尝试使用此API获取CSV文件并将其转换为TXT格式。

  

System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1+AsyncStateMachineBox 1 [System.String,StarRezToolApp.Program + d__2]

public static void GetReportInformation(string file_path_1, string Filename)
    {

        Utility.Utility.Log("TestFIle Reached");
        var report_data = HTTP_GET();

        Console.WriteLine(report_data.ToString());

        var sb_csv = new StringBuilder();

        try
        {
            if (File.Exists(file_path_1 + Filename))
            {
                using (StreamWriter apiresponse = File.AppendText(file_path_1 + Filename))
                {
                    apiresponse.Write(report_data.ToString());
                    apiresponse.WriteLine();
                }
            }
            else
            {
                using (StreamWriter apiresponse = new StreamWriter(file_path_1 + Filename))
                {
                    apiresponse.Write(report_data.ToString());
                    apiresponse.WriteLine();
                }
            }


            Utility.Utility.Log("File Created Successfully.");

        }
        catch (Exception ex)
        {
            Utility.Utility.Log("Error: Could Not Convert. Original error: " + ex.Message);
        }

    }

我一直在调用以下方法获取其他信息

private static async Task<string> HTTP_GET()
    {
        var TARGETURL = Properties.Resources.URL + Properties.Resources.Report_Name;


        Console.WriteLine("GET: + " + TARGETURL);
        Utility.Utility.Log("GET: + " + TARGETURL);

        NetworkCredential credentials = new NetworkCredential(Properties.Resources.Username, Properties.Resources.Tocken.ToString());
        HttpClientHandler handler = new HttpClientHandler 
        { 
            Credentials = credentials 
        };

        // ... Use HttpClient with handlers which has credentials
        HttpClient client = new HttpClient(handler);

        HttpResponseMessage response = await client.GetAsync(TARGETURL);
        HttpContent content = response.Content;

        // ... Check Status Code         
        Utility.Utility.Log("Response StatusCode: " + (int)response.StatusCode);                                 
        Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);

        // ... Read the string.
        string result = await content.ReadAsStringAsync();

        // ... Display the result.
        if (result != null && result.Length >= 50)
        {

            Utility.Utility.Log("Response message: Successful");
            return result.ToString();
        }
        else
        {
            Utility.Utility.Log("Response message: " + response.Content);
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

谢谢@RuardvanElburg先生。我得到了您的帮助。

我的控制器方法GetReportInformationAsync需要等待响应才能退出。