Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call REST services from Portable Class Library?

My target projects are Windows 8, WinRT and Windows Phone 8. I am using Portable Class Libraries for the solution in order to share the sources. I need to call some REST services created in another MVC Web Api project but HttpClient class is not available in the PCL. Which would be a good approach to solve it? I was thinking in a service interface and then to create a project for each platform, using HttpClient, WebClient or the appropriate class in each case. This approach could work but I was wondering if there are other ways.

like image 514
Dabiel Kabuto Avatar asked Jan 07 '13 13:01

Dabiel Kabuto


People also ask

Is PCL deprecated?

Portable Class Libraries (PCLs) are considered deprecated in the latest versions of Visual Studio.

What is portable class library?

The Portable Class Library project enables you to write and build managed assemblies that work on more than one . NET Framework platform. You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.

What is the difference between .NET Standard and PCL portable class libraries?

NET Standard is platform-agnostic, it can run anywhere, on Windows, Mac, Linux and so on. PCLs can also run cross-platform, but they have a more limited reach. PCLs can only target a limited set of platforms.

What is rest library?

The REST library is stateless. It is a wrapper around our REST API, however it provides a lot of additional functionality including authentication, routing around network partitions or data center availability issues, encryption and binary encoding.


2 Answers

Microsoft has rewritten the HttpClient library to be portable (PCL) and it's here on NuGet. For the time being, it's only available as pre-release so if you're using the NuGet GUI package manager make sure you set to "Include Prerelease". From the command line:

Install-Package Microsoft.Net.Http
like image 165
Sixto Saez Avatar answered Oct 14 '22 07:10

Sixto Saez


HttpClient is not necessarily portable from Microsoft's view. The only other way is to create separate libraries that each project will implement, and do as you have described. This way, you can achieve commonality across all the platforms you desire to target.

I thought there was some level of equivalency when it came to the platforms you are targeting... If you have to have separate projects, you could link your code files to the other project so you don't have to maintain multiple projects, or even use Project Linker (though I don't know if a 2012 version exists).

like image 24
Brian Mains Avatar answered Oct 14 '22 06:10

Brian Mains