【问题标题】:Error message "Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' "when running the published .exe运行已发布的 .exe 时出现错误消息“无法加载类型‘System.ComponentModel.AsyncCompletedEventArgs’”
【发布时间】:2021-02-05 16:08:09
【问题描述】:

我创建了一个小的 VB.NET 控制台应用程序(使用 Visual Studio 2019 社区版),它会自动从 Internet 下载文件。

如果我从项目树中的 Debug 目录启动已编译的 .exe,它就可以正常工作;如果我将应用程序发布到本地文件夹,则不会;相反,我收到错误消息:

Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.2.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

这是我用来发布我的应用程序的配置:

显然,这是产生异常的代码:

在程序模块中:

Imports System
Imports Microsoft.VisualBasic
Imports Microsoft.Win32

Module Program
    
     Sub Main(Args() As String)
        
        (...)
    
    End Sub

     
    Sub Download_SpeedTest()
        Dim ooklaWebSite As String
        Dim html As String

        ooklaWebSite = "https://www.speedtest.net/apps/cli"

        Try
            ' Retrieve download link
            Console.WriteLine("Getting download link from " & ooklaWebSite)
            html = Get_WebPage(ooklaWebSite)
        
        (...)
        
        Catch ex As Exception
            Console.WriteLine("An error occurred downloading SpeedTest utility. - " & ex.Message)
                    
        End Try
        
    End Sub

在 WebRequests 模块中:

Imports System.Net

Module WebRequests

    Public Function Get_WebPage(WebURL As String) As String
        Dim sourceString As String
        Dim wc As New System.Net.WebClient

        sourceString = wc.DownloadString(WebURL)
        wc.Dispose()

        Return sourceString

    End Function

End Module

这是我从 Windows 命令行运行已发布程序时得到的示例:

Getting download link from https://www.speedtest.net/apps/cli
An error occurred downloading SpeedTest utility. - Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

据我所知,只要调用函数Get_WebPage 就会发生异常(其中没有执行任何代码)

有什么建议吗?

【问题讨论】:

    标签: vb.net exception download


    【解决方案1】:

    当您使用Trim unused assemblies (in preview) 时,它有时会删除您实际需要的程序集。您可以将其关闭(增大文件大小)或针对此特定错误,您可以将以下内容添加到您的 csproj 文件中:

    <ItemGroup>
      <TrimmerRootAssembly Include="System.ComponentModel.EventBasedAsync" />
    </ItemGroup>
    

    这将确保 System.ComponentModel.EventBasedAsync.dll(其中定义了 System.ComponentModel.AsyncCompletedEventArgs)不会从最终的自包含二进制文件中删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2019-05-01
      • 2020-06-15
      • 2019-11-02
      • 2021-11-14
      • 2014-12-03
      相关资源
      最近更新 更多