如果无法加载,如何在VB.Net中下载DLL程序集

时间:2012-03-02 17:30:35

标签: vb.net

是否可以下载* .dll,然后在本地计算机上找不到它时加载它?我正在使用VB.Net,我可以捕获AssemblyResolve事件,但是当我尝试使用System.Net.WebClient从我的Web服务器下载程序集然后使用Reflection加载它时,我得到一个异常。

AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf downloadAssembly

Private Function downloadAssembly()
Dim downloader As New System.Net.WebClient()
downloader.DownloadFile(New Uri("http://path.toserver.com/myassembly.dll"), "myassembly.dll")
Dim NewAssembly As Reflection.Assembly
NewAssembly = Reflection.Assembly.LoadFrom("myassembly.dll")
Return NewAssembly
End Function
The exception is of type: System.InvalidOperationException
Problem signature:
  Problem Event Name:   CLR20r3
  Problem Signature 01: wind-x.exe
  Problem Signature 02: 1.0.0.0
  Problem Signature 03: 4f5104ea
  Problem Signature 04: Wind-X
  Problem Signature 05: 1.0.0.0
  Problem Signature 06: 4f5104ea
  Problem Signature 07: 1f
  Problem Signature 08: c6
  Problem Signature 09: System.InvalidOperationException
  OS Version:   6.1.7601.2.1.0.256.4
  Locale ID:    1033
  Additional Information 1: 0a9e
  Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
  Additional Information 3: 0a9e
  Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

1 个答案:

答案 0 :(得分:0)

我知道这是一个非常古老的问题,但这个问题对我和其他人来说仍然很有趣。

我刚试过你的代码,它对我有用。您不太清楚添加处理程序的位置。我尝试添加这样的应用程序事件:

Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
  AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf Form1.downloadAssembly
End Sub

由于您的异常消息对我来说不是很清楚,请尝试为未捕获的异常添加以下应用程序事件处理(或为您的程序集加载添加适当的try / catch):

Private Sub MyApplication_UnhandledException(sender As Object, e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
  MsgBox(e.Exception.Message)
End Sub

我查找了Uri-contstructor,WebClient.DownloadFile和Assembly.LoadFrom的文档。如果我没有错过任何东西,这些方法都不会引发InvalidOperationException。所以我只能疯狂地猜测你可以尝试的东西:

  1. 尝试使用不同的装配体,确保它不是装配体本身,导致问题。我可以想象,程序集框架和应用程序框架版本中的不匹配可能会造成一些麻烦。
  2. 尝试从文件夹启动应用程序,该文件夹具有最大权限,可以排除与用户权限相关的任何问题。如果可行,您可能需要用户提升。
  3. 如果所有这些都没有为您提供线索,请尽量使用应用程序和程序集中的代码尽可能少的代码。
相关问题