linux - 将带有类库的 .Net Core MVC 应用程序部署到 Ubuntu 16.04 :

标签 linux ubuntu asp.net-core asp.net-core-mvc

我想构建一个简单的 .net Core MVC 应用程序,其中包含一个用于数据访问的类库,以了解在 Linux 上构建/部署/运行 Core 需要什么。不像我希望的那么简单!

编辑:如果有人感兴趣,这是解决方案的副本: https://s3.amazonaws.com/kilonova-public/code/CoreCrossPlatform.zip

我将 VirtualBox VM 与 Ubuntu Server 16.04 放在一起,并按照以下说明安装了 dotnet 核心:

https://www.microsoft.com/net/core#ubuntu

我在主机 (Win10) 上为 VS 2015 安装了所有最新的、必要的部分,并创建了一个带有 MVC 应用程序和名为“DataAccess”的单一类库的解决方案。它是 EF Core 使用他们最新的核心提供者与 MySQL 对话。当我运行/调试它时,它在 Win10 主机上完美运行。提取数据,看起来很棒。

主机和虚拟机上的“dotnet --version”给我:

1.0.0-preview2-003121

但是,当我将它部署到 Ubuntu VM 时,我在类库依赖项上收到以下错误:

Project corecrossplatform (.NETCoreApp,Version=v1.0) will be compiled because the version or bitness of the CLI changed since the last build
Compiling corecrossplatform for .NETCoreApp,Version=v1.0
/opt/dotnet/corecrossplatform/project.json(24,23): error NU1002: The dependency DataAccess  does not support framework .NETCoreApp,Version=v1.0.

Compilation failed.
    0 Warning(s)
    1 Error(s)

Time elapsed 00:00:00.0187782

无论我运行“dotnet restore”还是“dotnet run”都会发生这种情况。老实说,我什至不确定我是否正确部署了这个东西。文档参差不齐,我正在做一些猜测。我将项目文件夹“src\CoreCrossPlatform”(包含 bin、Program.cs、appsettings.json 等)中的所有内容复制到 VM 上,这就是我在 VM 中执行“dotnet”命令的地方。

DataAccess .json 文件:

{
  "version": "1.0.0-*",

    "dependencies": {
        "Microsoft.EntityFrameworkCore": "1.0.0",
        "MySql.Data.Core": "7.0.4-IR-191",
        "MySql.Data.EntityFrameworkCore": "7.0.4-IR-191",
        "NETStandard.Library": "1.6.0"
    },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

MVC project.json:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "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",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "DataAccess": "1.0.0-*"
  },

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

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

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

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

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

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

需要注意的事项:当我使用“dotnet new”运行示例项目并恢复时,运行上面的教程链接,它运行良好。

我错过了什么?另一个问题:将此类应用程序发布到 Linux 机器的最佳方式是什么?我在这方面还算接近吗?

非常感谢。

编辑:整个下午我都在处理这匹死马,我比较了一些我在网上找到的与这个“NU1002”错误相关的注释,并生成了示例项目“dotnet new”。我尝试将两个 project.json 文件(MVC 和 classlib)的“框架”部分更改为以下内容,但没有成功...同样的错误:

"frameworks": {
    "netcoreapp1.0": {
        "dependencies": {
            "Microsoft.NETCore.App": {
                "type": "platform",
                "version": "1.0.0"
            }
        },
        "imports": [
            "dnxcore50",
            "dotnet5.6",
            "portable-net45+win8"
        ]
    }
}

编辑:非常感谢 goaty:正如他在评论中指出的那样,复制整个解决方案并构建它,结果构建成功。但是,我无法在没有错误的情况下运行它。它似乎没有恢复 MySQL EF Core 依赖:

Project CoreCrossPlatformFlat (.NETCoreApp,Version=v1.0) will be compiled because the version or bitness of the CLI changed since the last build
Compiling CoreCrossPlatformFlat for .NETCoreApp,Version=v1.0
/opt/dotnet/corecrossplatform/src/CoreCrossPlatformFlat/project.json(25,52): error NU1001: The dependency MySql.Data.EntityFrameworkCore >= 7.0.4-IR-191 could not be resolved.

Compilation failed.
    0 Warning(s)
    1 Error(s)

最佳答案

DataAccess 库位于您的 src/目录之外。因此,Web 项目找不到引用。 我推荐这种结构

src/
|---DataAccess/
|---CoreCrossPlatform/

希望对您有所帮助:)

关于linux - 将带有类库的 .Net Core MVC 应用程序部署到 Ubuntu 16.04 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318265/

相关文章:

Linux Mint 上的 Python3 错误 "no module named bluetooth"

regex - 在 Bash 脚本中返回正则表达式匹配,而不是替换它

linux - Perl - 自动将用户名和密码添加到 Linux 系统的脚本

docker - Rancher 没有启动。等待 Kubernetes API

ubuntu - GitLab 更新后 Nginx 无法启动

c# - 如何在C#中缓存具有多个键的对象

asp.net-core - 在 .NET 中配置后如何访问 AWS secret ?

c# - ASP.NET Core 3.0 中发生错误请求错误时显示错误属性

linux - Linux Bash shell 脚本中的 IO 重定向不重新创建移动/删除的文件?

ubuntu - "add-to list: command not found"在 ubuntu 中