【发布时间】:2019-10-15 16:10:02
【问题描述】:
我正在创建一个 API 使用工具,但在尝试调用 API 时出现以下错误。请帮我解决一下这个。我正在尝试使用此 API 获取 CSV 文件并转换为 TXT 格式。
System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1+AsyncStateMachineBox1[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;
}
}
【问题讨论】:
-
完整的堆栈跟踪是什么?
-
我没有得到任何堆栈跟踪。它只是给出了这个 System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1+AsyncStateMachineBox1[System.String,StarRezToolApp.Program+d__2] -
您应该在异步任务 GetReportInformation 中等待 HTTP_GET()。
-
我试过@RuardvanElburg。它现在什么都不给。该程序从该行中断调试。
-
@DigantJani 您还必须等待 GetReportInformation 等,直到控制器级别。还是 GetReportInformation 是控制器方法?