【问题标题】:Can't import netstandard1.6 project into netcore1.0 app无法将 netstandard1.6 项目导入 netcore1.0 应用程序
【发布时间】:2017-04-19 18:10:07
【问题描述】:

我创建了一个全新的 Asp.Net Core WebApi 应用程序和一个我打算在该应用程序中引用的 .NET Core 库。

  • 应用程序是 netcoreapp1.0
  • 库是 netstandard1.6
  • dotnet --version 为 1.0.0-preview2-1-003177

我已经在应用程序中引用了该库,但是该应用程序似乎无法解析该库中的任何内容。

  • 谁能向我解释这有什么问题?我认为它应该开箱即用?
  • 谁能解释我需要做什么才能让它跨平台运行?前 win10 和 ubuntu ?

网络 api 应用程序

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "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"
    },

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

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50"
        //"dotnet5.6",
        //"portable-net45+win8"
      ],
      "dependencies": {
        "PoC.ServiceDiscovery": {
          "version": "1.0.0-*",
          "target": "project"
        }
      }
    }
  },

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

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

  "runtimes": {
    "win10-x64" :  {} ,
    "ubuntu.16.04-x64":  {} 
  },

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

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

图书馆

{
  "version": "1.0.0-*",
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Microsoft.Extensions.DependencyInjection": "1.0.0"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  },
  "runtimes": {
    "win10-x64": {},
    "ubuntu.16.04-x64": {}
  }
}

问候

【问题讨论】:

  • 我认为这应该是另一个问题:谁能解释我需要做什么才能让这个跨平台运行? ex win10 和 ubuntu ?
  • 请粘贴两个项目的project.json
  • 我认为值得一提的是,尽管来自 StackOverflow 的样式,但基于 // 的 cmets 不是有效的 JSON。我很惊讶你在这种情况下的作品。
  • 你能把这个发布到 github 上吗?
  • 当然,给我几分钟

标签: .net asp.net-core .net-core


【解决方案1】:

(1) 从库的 project.json 中删除 runtimes。您的库正在定义一组它使用的 API(.NET Standard 1.6 API)。使用的那些 API 的实现由取决于您的库的运行时选择。它可能适用于列出的它们,但对于 .NET Standard 库没有任何意义。

(2) 您的库缺少名称。给它一个名字,这样你就可以在你的 WebAPI project.json 中引用它。为了清楚起见,我假设它将被称为PoC.ServiceDiscovery

(3) 将PoC.ServiceDiscovery(假设这是您的库的名称)放入WebAPI 的project.json 文件中dependencies 的正常列表中,紧邻Microsoft.* 库。

(4) 你需要让你的 WebAPI 项目知道在哪里可以找到这个库。有两种主要方法可以做到这一点。一种方法是使用 NuGet 或 MyGet 之类的包管理器共享库(这需要更新 NuGet.config 文件),或者我认为您想要在您的场景中执行的本地项目参考。在这种情况下,在您的根目录中添加一个 global.json 文件,其中包含一组包含您的库和 WebAPI 项目的相对路径,并使 WebAPI 项目中的依赖项成为项目引用。这意味着它看起来像这样:"PoC.ServiceDiscovery": { "target": "project" }

如果你想看一个例子,here is a very simple library 我已经写了uses .NET Standard for the library,一个project reference 用于库的测试项目,还有一个global.json file 允许dotnet 命令行工具知道运行测试时在哪里查找库。

【讨论】:

  • 如果你真的想亲自动手,克隆存储库,运行dotnet restore,然后运行dotnet test -f netcoreapp1.0 ./test/Invio.Hashing.Tests
  • 我在添加项目引用时收到“字典中不存在给定键”错误
  • 在做什么?如需一般调试指导,请将dotnet --version 信息添加到您的问题中(同时确保您拥有最新版本),并在您更新project.json 文件时运行dotnet restore
  • "PoC.ServiceDiscovery": { "target": "project" }, 添加到依赖项中,会使包还原失败并出现该错误
  • dotnet --version 信息添加
【解决方案2】:

起初不再分离 ASP.NET WebApi,因为它现在是 ASP.NET MVC 的一部分

但回到主题 - .NET Standard 只是更高级别的 API 抽象。支持 .NET Core 1.0 并不意味着可以跨平台运行(因为它还支持 .NET Framework、通用 Windows 平台等)。所以你在图书馆有两种方法可以做:

  • 删除 netstandard1.6 并替换 netcoreapp1.0
  • 删除运行时

更多信息我建议阅读https://github.com/dotnet/standard/blob/master/docs/versions.md

【讨论】:

  • 我认为我需要运行时才能在选定的平台上运行它?我应该用什么替换 netcoreapp1.0?
  • @Maciej 目标运行时在主项目中,而不是在使用抽象 API .NET 标准的库中
【解决方案3】:

 <ItemGroup>
    <ProjectReference Include="..\YourProjectName\YourProjectName.csproj" />
  </ItemGroup>

到目标 .csproj 项目。

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多