【问题标题】:How to find the NotSupportedException root cause?如何找到 NotSupportedException 的根本原因?
【发布时间】:2019-11-25 16:27:05
【问题描述】:

我有一个针对 .net 4.6.1 项目运行的 netcoreapp2.0 测试项目。安装 3.0 SDK 后,我开始在该项目的所有测试中引发 NotSupportedException。在只有旧版 SDK 或在 global.json 文件中指定 2.2 SDK 的机器上,它可以工作。

我的意思是,我怎样才能找到引发异常的原因?它被扔在我不拥有的 DLL 中,我无法使用 sourcelink。 VS 不显示任何堆栈跟踪,即使为每个异常都设置了 CLR 异常,它也不会停止异常。

这是我通过运行测试得到的唯一结果。

PS (...)> dotnet test
Test run for (...).Tests.dll(.NETCoreApp,Version=v2.2)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:01.35]     (...) [FAIL]
  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
[xUnit.net 00:00:01.43]     (...) [FAIL]
  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

  X (...) [1ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

(REPEATS FOR ALL THE TESTS...)

Test Run Failed.
Total tests: 30
     Failed: 30
 Total time: 3,2953 Seconds

更新: 我不知道在将 VS 更新到 16.3.9 (我在 16.3.8 上)和/或重新启动机器后如何或为什么它不再发生。所有测试现在都可以正常工作,无需对源进行任何修改。

git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
Test run for (...).Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.

Test Run Successful.
Total tests: 431
     Passed: 431
 Total time: 10,5425 Seconds

虽然我无法在完全相同的场景中重现错误,但我可以故意收到相同的错误消息,并且可能是相关的。 我们所有的测试都用Xunit.TheoryAttribute、几个Xunit.TraitAttribute和许多JsonDataSourceAttribute装饰。最后一个是内部开发的,它从 JSON 文件创建一个测试场景,非常像 DataSourceAttribute 所做的(就像在 How to: Create a data-driven unit test 上解释的那样)。 即使激活“抛出时中断”并在测试方法和构造函数上添加断点,当找不到测试场景文件时,我也会收到相同的错误消息并且没有提供堆栈跟踪。

TestClassName
   Source: (...)Test.cs line 35
   Duration: 1 ms

  Message: 
    System.NotSupportedException : Specified method is not supported.

也许之前的问题与未复制到输出的文件有关,但我仍然不知道为什么我没有得到堆栈跟踪或更具体的与未找到文件相关的错误。我将检查属性源并验证它是否可以改进,以及是否以某种方式隐藏了问题。

【问题讨论】:

  • 调试的时候可以停在抛出异常的位置。
  • 它只在断言上停止(可能是因为 IEnumerable 计算),我没有得到堆栈跟踪,只有这条消息。
  • 您是否尝试过将其包装在 try-catch 中并在调试模式下观察抛出的异常?
  • 奇怪的是调试器不会中断抛出的异常,即使它设置为中断,如你所说。会不会是在另一个进程中抛出了异常,而被调试的进程只是收到了某种关于它的通知?可能是您没有正确设置调试器在抛出异常时中断(在 Ctrl+Alt+E 窗口中)?
  • 顺便说一句,您还必须在“工具”>“选项”>“调试”>“常规”中取消选中“仅启用我的代码”。

标签: c# .net-core .net-core-3.0


【解决方案1】:

通过将JsonDataSourceAttribute 源添加到同一个解决方案中,我可以让它停在它应该停在的地方(在这种情况下,在“找不到文件”上)。

这是原始来源。在if (!File.Exists(inputfile)) 上抛出异常(强制发生时),但Xunit.Sdk.DataAttribute 将其隐藏。

    public sealed class JsonDataSourceAttribute : DataAttribute
    {
        public string InputFilePath { get; }

        /// <summary>
        /// Retrieves a collection of context data
        /// </summary>
        /// <param name="inputFilePath">Json source file path</param>
        public JsonDataSourceAttribute(string inputFilePath)
        {
            this.InputFilePath = inputFilePath;
        }

        public override IEnumerable<object[]> GetData(MethodInfo testMethod)
        {
            if (testMethod == null) { throw new ArgumentNullException(nameof(testMethod)); }

            var inputfile = Path.IsPathRooted(this.InputFilePath)
                ? this.InputFilePath
                : Path.Combine(Directory.GetCurrentDirectory(), this.InputFilePath);

            if (!File.Exists(inputfile))
            {
                throw new ArgumentException($"Input file not found at '{inputfile}'.");
            }

            return this.LoadFileData(inputfile, this.EnumMap);
        }

        (...)
    }

通过将文件存在检查移至构造函数,我能够得到我想要的。

        public JsonDataSourceAttribute(string inputFilePath)
        {
            var inputfile = Path.IsPathRooted(inputFilePath)
                ? inputFilePath
                : Path.Combine(Directory.GetCurrentDirectory(), inputFilePath);

            if (!File.Exists(inputfile))
            {
                throw new ArgumentException($"Input file not found at '{inputfile}'.");
            }

            this.InputFilePath = inputfile;
        }

这不是“找到根本原因”的答案,但我想这是不可能的,因为 Xunit.Sdk.DataAttribute 上省略了原始异常。

【讨论】:

  • 谢谢,这把我带到了正确的地方,json文件需要一个“内容”的构建动作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多