Cannot reference .NET Core library from UWP

时间:2016-07-11 20:04:25

标签: c# .net nuget uwp .net-core

I have a .NET Core library with the following project.json:

{
  "version": "1.0.0-*",
  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.6": { }
  },
  "scripts": {
    "postcompile": [
      "dotnet pack --no-build --configuration Release",
      "xcopy bin\\Release ..\\..\\lib\\ /Y"
    ]
  }
}

where the postcompile script creates a nuget package which I added as a custom feed in VS, following these instructions. This is because I want to reference it from a Windows Universal App, which cannot be otherwise (yet), according to this question. But when I try it, I get this message:

Package AEther 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0).
Package AEther 1.0.0 supports: netstandard1.6 (.NETStandard,Version=v1.6)
One or more packages are incompatible with UAP,Version=v10.0.

This is where it stops making sense to me. According to this, it should work fine for netstandard >=1.6.0, while this official table says I need to target netstandard <=1.4.0, but that doesn't change anything. More confusingly, if I downgrade both versions of netstandard (the dependency and the target framework) to, say, 1.5, I still get this exact same error without specifying 1.6 in any of my files.

Update The UWP project.json looks like this

{
  "dependencies": {
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.1"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

Can someone clear up either

  1. How to reference .Net Core libraries from UWP at all or
  2. What is going on in my specific case?

ANSWER

I solved it adding an import to the UWP app as follows:

{
  "dependencies": {
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.1"
  },
  "frameworks": {
    "uap10.0": { import [ "netstandard1.6" ] }
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

2 个答案:

答案 0 :(得分:10)

您需要将Microsoft.NETCore.UniversalWindowsPlatform升级到5.2.1

7月15日更新

好的,这是我的结果

  1. 创建新的UWP
  2. 升级到5月14日发布的5.2.2
  3. 更新project.json,import&#34; netstandard1.6&#34;

    { "dependencies": { "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2", "Test": "1.0.0" }, "frameworks": { "uap10.0": { "imports": [ "netstandard1.6" ] } }, "runtimes": { "win10-arm": {}, "win10-arm-aot": {}, "win10-x86": {}, "win10-x86-aot": {}, "win10-x64": {}, "win10-x64-aot": {} } }

  4. 创建一个新的dotnet核心库

  5. 构建库,并生成nuget包
  6. 我可以引用.dll文件或nuget包。而 在输入代码时变得聪明
  7. UWP已成功构建和部署,但一旦运行它,就会抛出异常 enter image description here

答案 1 :(得分:7)

  

这是我不再理解的地方。根据这个,它   应该适用于netstandard&gt; = 1.6.0,而这个官方表   说我需要针对netstandard&lt; = 1.4.0,但这并没有改变   任何东西。更令人困惑的是,如果我降级两个版本的   netstandard(依赖和目标框架),比方说,1.5,I   仍然得到这个完全相同的错误,而没有在我的任何一个指定1.6   文件。

Universal Windows Platform maps to netstandard1.4 - 既不是1.6也不是1.5。因此,您的库(我认为称为AEther)的要求比您的UWP应用程序要高。

  
      
  1. 如何从UWP中引用.Net核心库或
  2.   

your linked SO question中所述,Visual Studio中尚不支持此功能。

我只能猜测它与CLI的支持有关,即an open issue。截至今天,预计将在Microsoft.NETCore.UniversalWindowsPlatform元数据包的5.3版中修复 - 尽管之前预计会在5.2.2中修复。

  
      
  1. 我的具体情况是怎么回事?
  2.   

NuGet告诉您,您的软件包仅支持netstandard1.6 target framework,但不支持uap10.0。实际上,如果您打开.nupkg的密码,就会在lib\netstandard1.6下找到您的DLL。

由于dotnet pack会自动从.nuspec创建project.json,因此您需要使用适当的框架(例如netstandard1.4)进行修复。对于不同的框架,它可能更容易编译,例如Portable profiles compatible with .NET Platform Standard