Alec
Alec

Reputation: 31

Reference Portable Class Library (PCL) from ASP.NET Core Web API

I am using Visual Studio 2015. I have created a PCL project. Class Library Portable (IOS, Android and Windows). This PCL is a simple POCO class.

It targets: .NET Framework 4.5, ASP.NET Core 1.0, Windows Phone Silverlight 8, Xamarin.Android, Xamarin.IOS

My PCL is referenced by a Xamarin.IOS app and I want to use the same PCL in my Web API.

I am trying to reference this PCL project from an ASP.NET 5 Web API.

The following is in my project.json:

"dnx451": { "dependencies": { "MyPCL": "1.0.0-*" } }

But I get this error:

Error: The Dependency could not be resolved.

I have tried adding the following under frameworks too but no luck.

".NETPortable,Version=v4.0,Profile=Profile111": {

    "dependencies": {
         "MyPCL": "1.0.0-*"
       }
     
  "frameworkAssemblies": {
    "mscorlib": "",
    "System": "",
    "System.Core": ""
  }
}

I have also tried dnu restore... still does not work.

So I upgraded to RC2, but it does not resolve the issue. In project.json now I have:

   "dependencies": 
   {
       "NETStandard.Library":  "1.5.0-rc2-24027"
   } 

and I added the following under framework:

"netstandard1.5": {
  "imports": "dnxcore50"
}

when I compile my Web API project I get the following 3 errors:

  1. the project has not been restored or restore failed. (though I did run dotnet restore)

  2. The project does not list one of 'win10-x64, win81-x64, win8-x64, win7-x64' in the 'runtimes' section.

  3. Cannot find runtime target for framework '.NETStandard,Version=v1.5' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'

Does anyone know how to resolve this issue?

Is it even possible to reference this PCL from an ASP.NET Core Web API or MVC 6 project?

Upvotes: 3

Views: 540

Answers (1)

Jon Douglas
Jon Douglas

Reputation: 13176

I think your best approach is to:

Upgrade this PCL to a netstandard 1.3+ library. This however would force you to create/upgrade to a netcore 1.0+ MVC/API application to consume as interface INetCoreApp10 : INetStandard15 is the best analogy to think of with netcore projects(Current version is 1.1).

For more information on how to upgrade, you can view the following: https://blog.xamarin.com/net-standard-library-support-for-xamarin/

Please note that if you do this, you can also use the Compatibility Shim to reference other PCLs inside the netstandard library.

https://learn.microsoft.com/en-us/dotnet/articles/standard/library#pcl-compatibility

To answer your question:

Is it even possible to reference this PCL from an ASP.NET 5 Web API or MVC 6 project?

Yes it is. There are many ways to do it and I personally think the correct way is to use the "new" bits such as netstandard and netcore in this situation. Based off my answer you have multiple things you can do:

  1. Upgrade the PCL to netstandard and the API project to netcore. The netcore project can easily consume the netstandard project.
  2. Upgrade the API project to netcore and use the Compatibility Shim to reference old PCL Profiles.
  3. Stay using pre-netcore/netstandard project types and consume the PCL in a ASP.NET MVC 5 Web API project. This is also known as a netframework project rather than netcore. i.e. https://learn.microsoft.com/en-us/dotnet/articles/standard/choosing-core-framework-server

Upvotes: 1

Related Questions