从.NET核心项目引用可移植类库

时间:2016-07-08 15:53:45

标签: c# asp.net portable-class-library .net-core

是否可以从.NET Core项目添加对PCL(针对.NET 4.6和.NET Core)的引用?

我尝试使用这两种项目类型创建解决方案,但如果我从.NET Core项目添加对PCL的引用,则它不会识别任何命名空间。它建立时没有任何问题。

以下是project.json文件:

PCL:

{
  "supports": {
    "net46.app": {},
    "dnxcore50.app": {}
  },
  "dependencies": {
    "Microsoft.NETCore": "5.0.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.0"
  },
  "frameworks": {
    "dotnet": {
      "imports": "portable-net452"
    }
  }
}

.NET Core:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "ClassLibrary1": { // <-- Reference to the PCL
          "target": "project"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

修改

为了它的价值,按照要求提供构建输出:

Restoring NuGet packages...
To prevent NuGet from restoring packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages during build.'
1>------ Build started: Project: ClassLibrary2, Configuration: Debug Any CPU ------
1>  C:\Program Files\dotnet\dotnet.exe build "C:\ClassLibrary2\ClassLibrary2" --configuration Debug --no-dependencies
1>  Project ClassLibrary2 (.NETStandard,Version=v1.6) will be compiled because inputs were modified
1>  Compiling ClassLibrary2 for .NETStandard,Version=v1.6
1>  Compilation succeeded.
1>      0 Warning(s)
1>      0 Error(s)
1>  Time elapsed 00:00:00.8914597
1>
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

编辑2:

我已经设法至少让它构建,但现在我得到一个关于它没有找到System.Runtime的运行时错误。奇怪的是,它正在测试解决方案,但不是我想要实现它的那个。

供参考,这是我的project.json文件:

PCL:

{
  "supports": {},
  "dependencies": {
    "Microsoft.CSharp": "4.0.1",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "System.Runtime.Serialization.Primitives": "4.1.1"
  },
  "frameworks": {
    "net451": {},
    "netstandard1.5": {}
  }
}

.NET Core:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "PclTest": {
          "target": "project"
        },
        "System.Runtime": "4.1.0"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

  

它无法识别任何名称空间

我相信这是因为Visual Studio被RC1和RTM项目的组合所迷惑,它确实成功编译(虽然我不知道结果是否真的可用)。

如果你想编写一个可以从.Net Core和.Net Framework中使用的库,你应该使用 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <jnlp codebase="file:/C:/Users/JuanCarlos/Documents/NetBeansProjects/HolaMundo/dist/" href="launch.jnlp" spec="1.0+"> <information> <title>HolaMundo</title> <vendor>JuanCarlos</vendor> <homepage href=""/> <description>HolaMundo</description> <description kind="short">HolaMundo</description> <offline-allowed/> </information> <update check="background"/> <security> <all-permissions/> </security> <resources> <j2se version="1.7+"/> <jar href="HolaMundo.jar" main="true"/> </resources> <application-desc main-class="holamundo.HelloWorld"> </application-desc> </jnlp> ,而不是netstandard

“PCL”ClassLibrary1的project.json应该与“.Net Core”ClassLibrary2中的一样。