user3074140
user3074140

Reputation: 857

No HttpClient in xamarin

I recently downloaded and installed xamarin studio for my mac. I then began following a tutorial for c# coding with xamarin as I am a complete beginner. During the tutorial they use a HttpClient and add

using System.Net.Http

to their project so they can use the HttpClient. I was unable to do the same and only had the option of

using System.Net.NetworkInformation

This was a PCL project. I also tried a shared library project which gave more options including Cache, Mail, Sockets, Websockets, Security etc.

Am I missing something or doing something wrong.

Upvotes: 3

Views: 610

Answers (3)

arsena
arsena

Reputation: 1975

I don't think you need any extra packages to access HttpClient class. I've been using it for almost two years already in all of my projects. Example:

enter image description here

What I suggest:

Go to: Edit References => Packages => Check System.Net.Http. It should be there.

If you can't find it there, or list is empty under Packages(some kind of a bug) then reference it manually from here: /Library/Frameworks/Mono.framework/Versions/4.4.2/lib/mono/4.5-api

Upvotes: 0

C.Evenhuis
C.Evenhuis

Reputation: 26436

This is what PCL is all about - by itself it contains only the most basic .NET functionality. You can write business logic and application flow in a PCL, but you can't reference most libraries that somehow interact with hardware, as this is platform-dependent.

There are two major solutions to this problem:

  • Dependency Injection: the PCL defines an interface, and your platform-specific assemblies provide an implementation of this interface - this implementation can then reference all libraries available to the platform.

  • Bait-and-switch: you create a PCL with all methods you require, that all throw NotSupportedException and include separate libraries with the same signature for each platform that have proper implementations of these methods. The compiler will only include the libraries of the specific platform. I personally consider this a bit of trick but this is how most NuGet packages provide their functionality directly available "as a PCL".

Upvotes: 1

Thibault D.
Thibault D.

Reputation: 10104

You need to add the Microsoft.Net.Http NuGet package.

Upvotes: 3

Related Questions