FileNotFoundException在没有进入方法的情况下在方法调用时抛出

本文关键字:方法 调用 情况下 FileNotFoundException | 更新日期: 2023-09-27 18:06:48

所以我遇到了一个真正令人困惑的FileNotFoundException,我不确定如何排除故障。

我有一个.cs文件,作为网页方法的基本模板。在其中,我有以下私有方法:

private void Initialize(object sender, EventArgs e)
    {
        // Some initialize functions and...
        AddWebAnalyticsScript();
    }

AddWebAnalyticsScript()是同一个类中的私有函数。它处理一些谷歌网络分析代码的加载。通过这个类调试,我看到AddWebAnalyticsScript()抛出了一个依赖程序集的FileNotFoundException。

我尝试在函数定义中放置断点,因为我假设其中的一些逻辑引用了丢失的程序集。然而,当我在函数定义中放置断点时,我发现在函数中执行任何代码之前抛出此异常;它在调用时立即发生。我以前从未见过这种情况,也找不到任何类似问题的文档。从概念上讲,我不明白为什么调用在同一类中定义的函数会寻找程序集,直到包含的代码实际执行。

此外,程序集似乎正好出现在它应该出现的地方。

所以我很困惑。除了在函数定义中放置断点之外,我想不出其他的故障排除方法。在AddWebAnalyticsScript()上使用调试器"step into"动作;导致立即抛出异常。

错误文本;

{"Could not load file or assembly 'myApplication.Frameworks.WebAnalyticsInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0113ab8eb0779932' or one of its dependencies. The system cannot find the file specified.":"myApplication.Frameworks.WebAnalyticsInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0113ab8eb0779932"}

堆栈跟踪:

at MyApplication.PageTemplate.AddWebAnalyticsScript()
at MyApplication.PageTemplate.Initialize(Object sender, EventArgs e) in D:''{filepath}:line 246
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Control.OnInit(EventArgs e)
at System.Web.UI.UserControl.OnInit(EventArgs e)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

From fusion log:

LOG: This bind starts in default load context.
LOG: Using application configuration file: D:'MyApp'Web'web.config
LOG: Using host configuration file: C:'Windows'Microsoft.NET'Framework'v2.0.50727'Aspnet.config
LOG: Using machine configuration file from C:'Windows'Microsoft.NET'Framework'v2.0.50727'config'machine.config.
LOG: Post-policy reference: MyApp.Frameworks.WebAnalyticsInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0113ab8eb0779932
LOG: The same bind was seen before, and was failed with hr = 0x80070002.
ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002).

FileNotFoundException在没有进入方法的情况下在方法调用时抛出

正如您自己所述,您定义的方法在方法内部执行代码之前不可能需要调用外部程序集。

您还提到,违规程序集实际上是直接引用的。这并不一定意味着可以定位该程序集的所有依赖程序集。

为了解决dll加载问题,总是使用Fusion Logging。我引用的文章包含有关如何打开和使用它的信息。这真的很简单。

程序集无法加载的原因可能有很多,包括错误的映像格式(x64 vs x86)、错误的搜索路径等。这些都可以与Fusion Log区分,因为它记录了不同路径上的所有尝试,并给出了特定组装失败的详细原因。