c# - 什么是 deps.json,如何让它使用相对路径?

标签 c# asp.net-core .net-core

我正在 TeamCity 上设置一个 ASP.NET Core 项目。它构建的二进制文件在其他机器上启动时崩溃。错误消息表明它正在仅存在于构建服务器上的路径中查找 dll。 DotPeek 显示 .exe 中有一个名为 myproject.deps.json 的嵌入式资源文件。在那里的目标部分中,有对使用绝对路径的 dll 的引用。这意味着 ASP.NET Core 二进制文件只会在构建它们的机器上运行。

如何解决这个问题?这个文件是什么,如何让它使用相对路径?经过一些挖掘,看起来路径来自 project.fragment.lock.json,这是一个生成的文件。如果我编辑它以使用相对路径,该文件将再次被覆盖。是什么导致了这种情况,如何修复或停止?

对于那些提问的人,project.json 看起来像:

{
  "dependencies": {
    "CommandLineParser": "1.9.71",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "System.Configuration.Abstractions": "1.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net461": {
      "dependencies": {
        "Company.Common": {
          "target": "project"
        },
        "Company.Integration": {
          "target": "project"
        },
        "Company.Functions": {
          "target": "project"
        },
        "Company.Utils": {
          "target": "project"
        }
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

最佳答案

第一个问题的答案,根据 Runtime Configuration File Documentation:

第二个问题的答案是删除 project.json 文件中的 preserveCompilationContext(并重建)。

MyApp.deps.json is a list of dependencies, as well as compilation context data and compilation dependencies. Not technically required, but required to use the servicing or package cache/shared package install features.

根据您对 Dimitry 的评论,我无法判断您是否在目标机器上安装了 .net 核心,因此无法推断您尝试执行的部署类型。但假设它已安装,您应该能够调整您的 myproject.runtime.json 来解决您的问题。如果您不这样做,我强烈建议您阅读两种不同类型的 .NET Core Application Deployment:

You can create two types of deployments for .NET Core applications:

Framework-dependent deployment. As the name implies, framework-dependent deployment (FDD) relies on a shared system-wide version of .NET Core to be present on the target system. Because .NET Core is already present, your app is also portable between installations of .NET Core. Your app contains only its own code and any third-party dependencies that are outside of the .NET Core libraries. FDDs contain .dll files that can be launched by using the dotnet utility from the command line. For example, dotnet app.dll runs an application named app.

Self-contained deployment. Unlike FDD, a self-contained deployment (SCD) does not rely on any shared components to be present on the target system. All components, including both .NET Core libraries and the .NET Core runtime, are included with the application and are isolated from other .NET Core applications. SCDs include an executable (such as app.exe on Windows platforms for an application named app), which is a renamed version of the platform-specific .NET Core host, and a .dll file (such as app.dll), which is the actual application.

关于c# - 什么是 deps.json,如何让它使用相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40849745/

相关文章:

c# - 将字符串格式化为 10 个字符

c# - BackgroundWorker 在 VSTO 中不工作

c# - 如何从 ASP.NET Core 中的 ActionFilterAttribute 访问 AppSettings

.net - dotnet 构建 UWP.sln |错误 MSB4019 Microsoft.Windows.UI.Xaml.CSharp.targets 未找到

c# - 日期类型 c#.net 上的比较验证器

c# - 混淆后的 .Net C# 应用程序可以被反编译吗?

entity-framework - "No executable found matching command "点网 ef ""

c# - User.IsInRole 在 ASP.NET Core 中不返回任何内容(已实现存储库模式)

c# - 在 Swagger 中添加多个 header 值

.net - .Net Core 的 System.String 的源代码在哪里?