【问题标题】:Unable to load sample data into dotnet core app when running in debugger but runs fine in terminal在调试器中运行时无法将示例数据加载到 dotnet 核心应用程序中,但在终端中运行良好
【发布时间】:2016-11-10 17:38:55
【问题描述】:

我有一个 dotnet 核心项目,我将重要的示例数据从 XML 导入。

XDocument sampleData = XDocument.Load(@"seed/Import_sampleData.xml");

在终端中,当我使用命令 dotnet run 执行它时,它会按预期加载数据,但在 Visual Studio Code 中,当我尝试运行调试器时会得到不同的结果:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not find file '/Users/Chris/Google Drive/Repos/project/project.api/seed/Import_sampleData.xml'.

我不确定问题出在哪里,我认为调试器也使用 dotnet run 那么为什么它的行为会有所不同?

正如这里所建议的,这是我在 project.json 中的 copyToOutput 设置。 xml 文件现在在构建时被复制到 bin 文件夹,但仍然返回文件未找到异常:

"copyToOutput": {
  "include": [
    "config.json",
    "Certificate.pfx",
    "seed\\"        
  ]
}

【问题讨论】:

  • 我认为当你从 Visual Studio 运行它时,它使用二进制文件夹作为基本文件夹,当你从控制台运行它时,它使用项目文件夹。您是否尝试将文件复制到构建目录(使用 project.json copyToOutput
  • 我将目录添加到 copyToOutput,当项目构建时,我现在可以在 bin 的子文件夹中看到 xml 文件,但我仍然得到相同的行为。找不到文件

标签: asp.net-core visual-studio-code omnisharp


【解决方案1】:

我想通了...基本上我有 2 个库项目,然后我的 api 项目都在一个子文件夹下。因此,调试器从包含三个项目的根工作区文件夹开始。我将 cwd 更改为在我的 api 项目中启动,因为其他项目是通过依赖项拉入其中的,然后它立即在我的项目中找到了 xml 文件。

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 2021-11-13
    • 2018-12-08
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多