System.Reflection.TargetInvocationException错误(C#)

问题描述 投票:2回答:1

我试图通过C#中的HtmlAgilityPack从网上获取信息(我的代码中的adress),但我必须等到页面上加载<div class="center fs22 green lh32">

当我执行此代码时:

    var url = $"https://www.website.com/";
    var web = new HtmlWeb();
    var doc = web.LoadFromBrowser(url, html =>
    {
        return !html.Contains("<div class=\"center fs22 green lh32\"></div>");
    });
    string adress = doc.DocumentNode
          .SelectSingleNode("//td/span[@id='testedAddress")
          .Attributes["value"].Value;
    Console.WriteLine("Voici l'adresse :",adress);

我总是得到这个错误:

System.Reflection.TargetInvocationException

ThreadStateException: Impossible d'instancier le contrôle ActiveX '8856f961-340a-11d0-a96b-00c04fd705a2', car le thread actuel n'est pas un thread cloisonné (STA, Single-Threaded Apartment).

翻译:System.Reflection.TargetInvocationException:'上诉目标已引发异常。'

ThreadStateException:无法实例化ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2',因为当前线程不是分区线程(STA,Single-Threaded Apartment)。

我怎样才能摆脱这个错误?

c# html-agility-pack
1个回答
3
投票

将STAThreadAttribute应用于您的Main函数:

 [STAThread]
 static void Main(string[] args)
 {
     //Your code here
 }
© www.soinside.com 2019 - 2024. All rights reserved.