Sdk: How do I reference .NET Core project from UWP?

Created on 22 May 2016  路  48Comments  路  Source: dotnet/sdk

Is it possible to reference .NET Core RC2 class library project from UWP project?

Most helpful comment

I found using VS 2017 .NET Core and UWP can both reference .NET Standard class library. Although I tried this before without success seems to work now in VS2017.

All 48 comments

@ericstj

You need to use the RC3 packages to make this work and update the UWP project to use the 5.2.0-rc3-* Microsoft.NETCore.UniversalWindowsPlatform package.

Can those (rc3 packages) be published to the store? I know there was some restrictions with preview apis and uwp apps previously

On which feed are they available?

@onovotny, there is no new versions there

That feed currently contains Microsoft.NETCore.UniversalWindowsPlatform v5.2.0, that is the version to use.

I cannot find it on public feed.
Where is it?

It's not released on NuGet yet. It's on https://dotnet.myget.org/gallery/dotnet-core and is in the process of stabilization. It will be released to NuGet on the timeline announced here for RTM: https://blogs.msdn.microsoft.com/dotnet/2016/05/06/net-core-rc2-improvements-schedule-and-roadmap/

Thanks, that works

Can someone explain me how to reference WinRT and UWP APIs from .NET Core library project?
https://github.com/dotnet/cli/issues/3561

I'm trying to add a reference to a "netstandard1.4" library project from a UWP project with "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.0" and get an error "Unable to add a reference to project 'ClassLibrary1'.". I have all the latest software installed like .Net Core 1.0 RTM and VS 2015 Update 3.

As far as I know, 5.3.0 is required

Previous advise was to use 5.2.0 (above)...

@ericstj Any update on the timeline? The RTM has been released, but UWP 5.2.0 still doesn't appear to be present on nuget.org.

@onovotny Thanks for that. So presumably that means unless I want to go through reasonably convoluted hoops, I should park my Shed Automation project (UWP consuming .NET Core libraries)? (Fortunately in this case there's no urgency...)

The hoops don't seem to bad, just disabling the shared library and excluding a specific package. That said, I'm sure they're working on a fix real soon :)

It also only affects store submission, not local development, so depending on when you're ready to submit, it may be already fixed directly by then.

Ping? Is a UWP 5.2.1 package coming soon with this all fixed?

@onovotny I upgraded to a 5.2.1 from the myget feed two days ago:
https://dotnet.myget.org/feed/dotnet-core/package/nuget/Microsoft.NETCore.UniversalWindowsPlatform

This seems to have addressed this issue: https://github.com/dotnet/corefx/issues/9743

Looks likt it's on NuGet now: https://github.com/dotnet/corefx/issues/9743#issuecomment-231898345

Hmm. I'm still left with this error in Visual Studio:

Your project.json doesn't list 'win10-x86' as a targeted runtime. You should add '"win10-x86": { }' inside your "runtimes" section in your project.json, and then re-run NuGet restore.

... but on the command line, dotnet restore works, but dotnet build doesn't find any of the UWP-specific libraries, e.g.

The type or namespace name 'ApplicationModel' does not exist in the namespace 'Windows' (are you missing an assembly reference?)

The UWP project.json is simply:

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

Are there further changes required?

As of now, dotnet build cannot build UWP/WinRT-based projects without hacks that work in only some cases.

If you're building a "pure" library, and do not have resources, then you can follow what Rx.NET did, here:
https://github.com/Reactive-Extensions/Rx.NET/blob/master/Rx.NET/Source/System.Reactive.WindowsRuntime/project.json#L109-L117

You'll need to create a "targeting pack" that contains the windows.winmd file. One core issue is that the windows.winmd file can change based on SDK and none of that has been taken into account yet.

That WinRT "targeting pack," with some version of windows.winmd, is on their MyGet feed: https://dotnet.myget.org/feed/dotnet-core/package/nuget/Microsoft.TargetingPack.Private.WinRT. I'd recommend copying it or building your own similar one.

Now the gotchas:

  • This won't work if you have resources. There's nothing in the build pipeline to create the PRI files
  • Also won't work if you have XAML pages (same issue, nothing to create the generated portion)

Righto - thanks for that, will give it a shot. Are any hacks meant to be needed when building with VS though?

(I don't have any resources or XAML or anything fancy in my library - it's just a netstandard1.0 library containing just code.)

I believe you'll need those targeting packs either way because even in VS, it still uses dotnet build to for the actual build step.

Question though -- why do you need a UWP target if it's a .NET Standard1.0 library? What are you trying to do?

I don't need a UWP target for the library - that's for the app.

I have the following projects:

  • Shed.CommandLine: netcoreapp1.0, depends on Shed.Controllers
  • Shed.Controllers: netstandard1.3 (not 1.0, sorry)
  • Shed.Uwp: uap10.0, depends on Shed.Controllers

Would you expect that to build under VS and/or dotnet? All the code is at https://github.com/jskeet/DemoCode/tree/master/Shed - I've just pushed the most recent attempt.

The CommandLine and Controllers projects both build fine - it's only the UWP project that doesn't.

@andriysavin If you want to do a project to project reference from a UWP project (which is a csproj) to an xproj, then my Stack Overflow post shows how to do that: http://stackoverflow.com/a/37907973/738188

Alternatively, you can also use a csproj PCL that's set to target .NET Standard 1.4 and a P2P reference should work normally.

Okay, thanks - will read the SO post. (All of this was fine under DNX. Not sure what I was doing differently then.)

Sounds like another option would be to host the controllers package in a local Nuget source and use that...

@jskeet yeah, from what I'm seeing, you have the same issue as well -- you cannot use "Shed.Controllers": { "target": "project" } in a UWP project.json. The only thing that should really be there are nuget packages.

You may consider making your Shed.Controllers a netstandard 1.3 csproj pcl and then a P2P ref will work normally. That'll bump your desktop net to be net46, but you could still use it in both a netcoreapp1.0 and a net46 command line.

@onovotny Thanks, I saw that SO answer and used that workaround (csproj PCL) - it works. But I hope for xproj this will work as expected too in the nearest future.

Right, I've gone for the "build a local package" option, and it works fine. Documented in the new readme :) Thanks yet again @onovotny.

If I reference standard PCL why do I get such error and how to fix it?
Your project is not referencing the ".NETPortable,Version=v4.5,Profile=Profile259" framework. Add a reference to ".NETPortable,Version=v4.5,Profile=Profile259" in the "frameworks" section of your project.json, and then re-run NuGet restore.

Update: Problem was in PCL, not UWP

Thanks for your help, @onovotny

"Microsoft.NETCore.UniversalWindowsPlatform": "5.3.0" still cannot reference .NetCore libraries. Any update on that?
Sorry, I have just now seen the error that 5.3.0 can only be used in Visual Studio 2017 (I tried on Visual Studio 2015 Update 3). In NuGet it was this mention for the beta release, but it was removed for the final one.
Can at least, please, someone confirm that in Visual Studio 2017 it works to reference a .NetCore Library from a UWP project?

Can at least, please, someone confirm that in Visual Studio 2017 it works to reference a .NetCore Library from a UWP project?

It seems to work if you add the reference "manually", but I can't get it to work with the generated NuGet Package. Maybe something still doesn't work properly since this feature was added in the current RC version.

It works for me in my Shed Automation project. Try it:
https://github.com/jskeet/DemoCode/tree/master/Shed

(Obviously unless you've got the exact same setup as me, it won't be useful, but you should be able to see it building...)

I still don't understand why a .NET Core NuGet Package can't be consumed in a UWP project. I can manually reference the .dll without problems but when I try to add the NuGet Package the output says:

The package xxx 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0)/win10-arm. The package xxx 1.0.0 supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
One or more packages are not compatible with UAP,Version=v10.0 (win10-arm).

@TommasoScalici It sounds like the package you're trying to use is netcoreapp1.1. That is a .NET Core library. .NET Core is a platform, not a surface area -- similar/in parallel to UWP, .NET, Xamarin, etc.

netcoreapp1.x can only be referenced by other netcoreapp libraries/apps.

I think what you want instead is a .NET Standard library, and that's netstandard1.x. .NET Standard 1.0-1.4 can be consumed by a UWP app/library.

@onovotny Thanks, that in fact was the mistake. I still make confusion between the two.

Hi Guys !
So if I understand, it's not yet possible to create a solution in which a .net core library will be referenced in an UWP project (and in an asp .net core project in the same time) ?

Is there any workaround ?

Thank you,
Olivier

Why not? Just create MSBuild 15 UWP project using Visual Studio 15.2, then you must be able to.

Well ... it is actually what I am trying to do ...
In my VS2017 I have created a .NetCore Class Library, and a UWP Application. When I try to add my class library as Reference in my UWP Application, I have an error message : Unable to add reference.

Are you sure it is a MSBuild 15 project?

I just create my solution and my projects with VS2017 ... But if there is a way to check the project version number, can you please tell me how to do ?

Thank you very much for your help !

I found using VS 2017 .NET Core and UWP can both reference .NET Standard class library. Although I tried this before without success seems to work now in VS2017.

Hi Mark !
Thank you very much for the tip !

Do you think it will run on Windows IoT on my Rpi3 ?

I can't wait to test it now ;)

Hi guys, I've created just now project in UWP with VS 2017 which is surely MSBuild 15 project. but I still get same error "unable to add reference". Any suggestions?
Why there is no more info about this error :0

Was this page helpful?
0 / 5 - 0 ratings