Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference a class library from UWP and ASP.NET 5

I am trying to create a class library that'll contain common objects (mainly DTOs) of a WebAPI (using ASP.NET 5) and a consuming UWP App. However, I have not yet figured out how to create the class library such that it can be referenced from both other projects.

What I have tried so far:
First, I tried a Class Library (Package), which can be found under Web. This type of library can be referenced from the ASP.NET project without problems, but when trying to reference it from the UWP project, I get the following message:

A reference to 'ClassLibrary1' could not be added. 

Next, I tried a Class Library (Windows Universal), which can be found under Windows > Universal. This can easily be references from the UWP project, but when trying to reference it from ASP.NET, I get:

The following projects are not supported as references : 

  - The project ClassLibrary2 has a target framework that is incompatible or has version higher than the current project and cannot be referenced.

So: How can I create a class library that can be used in both an ASP.NET 5 project and an UWP project?

like image 751
Henrik Ilgen Avatar asked Dec 01 '15 19:12

Henrik Ilgen


People also ask

Does .NET 5 support UWP?

NET 5/6 will not be coming to UWP project types."

How do you add a reference to UWP project?

Restart Visual Studio, create a new C# UWP app project, and then right-click on the project and choose Add Reference.

How do I add a reference to .NET core library?

Add Class Library Reference To use a class library in your application, you must add a reference to the library to access its functionality. Right click on the project name of your console app in Solution Explorer and select Add ->Reference option.

Does .NET core support UWP?

You can run UWP on . NET Core today.


1 Answers

What you need is a Portable Class Library (under Windows).

PCL

Since you're only targetting ASP.NET 5 and Windows 10, you can limit the platforms to these two only, but there's no "error" in supporting more platforms (it can limit how much of the shared API is available though).

PCL 2

like image 118
Bart Avatar answered Sep 18 '22 20:09

Bart