简体   繁体   中英

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.

您需要添加Microsoft.Net.Http NuGet包。

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".

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:

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM