【发布时间】:2020-03-06 17:48:22
【问题描述】:
我正在尝试通过其 url 调用站点以构建 webscraper,但是当我尝试从等待的 url 中获取项目时,我的应用程序停止并显示以下消息:
blablah.exe (process 1512) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stop
我不认为这是代码问题,也许是我的 Visual Studio 调试器的问题?我一辈子都看不到它是什么。
这是一个 .net 核心命令行应用程序
一些代码以防万一
static void Main(string[] args)
{
GetHtmlAsync();
}
private static async void GetHtmlAsync()
{
var url = "https://blahblah.com";
var client = new HttpClient();
var html = await client.GetStringAsync(url);
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var newsList = htmlDoc.DocumentNode.Descendants("table")
.Where(node => node.GetAttributeValue("class","")
.Equals("itemList")).ToList();
Console.WriteLine(newsList);
Console.Read();
}
}
调试时它甚至永远达不到:
var htmlDoc = new HtmlDocument();
有什么想法吗?
【问题讨论】:
-
最可能的原因是未处理的异常。添加调试代码,例如带有错误记录的 try/catch。您还可以查看系统事件日志,通常情况下也会记录异常,但这取决于系统和配置的设置。
-
您在 Main 中启动了一个异步函数,然后退出了您的程序。这就像把蛋糕放在烤箱里,然后在蛋糕烤好之前立即卖掉烤箱。
-
0是正常退出。您无需等待GetHtmlAsync,因此应用程序在该方法有机会完成之前正常退出。 -
@Wyck 我的星期五晚上笑了,很好的比喻。
标签: c# .net-core command-line