System.MissingMethodException method "System.Net.Http.HttpClientHandler.set_Proxy" not found

This is a Xamarin solution and I am getting the error I found in this post header. Of course, I can easily confirm that the PCL project has a Proxy property on the HttpClientHandler. And the solution is built without errors. Only when I run it throws this error (on Droid or iOS) and does so at the point where it calls a method in the PCL that instantiates the HttpClient. Note that it doesn't even get to this method. The error appears when starting the application; for example UIApplication.Main ()

If I comment out the handler and instantiate the HttpClient without the handler, it works fine while I'm on the open internet. But I am trying to get this to work because of the proxy.

Further investigation showed that there are no references to System.Net.Http in device projects. So I added them - and it points to Xamarin.iOS and Xamarin.Android as packages, but it still throws an error.

I don't understand what the error is telling me, but I believe it means the device project cannot see the System.Net.Http.HttpClientHandler?

    private HttpClient GetHttpClient()
    {
        WebProxy proxy = new WebProxy(ProxyConfig.Url)
        {
            Credentials = new NetworkCredential(ProxyConfig.Username, ProxyConfig.Password)
        };

        // At runtime, when GetHttpClient is invoked, it says it cannot find the Proxy setter
        HttpClientHandler handler = new HttpClientHandler
        {
            Proxy = proxy,
            UseProxy = true,
            PreAuthenticate = true,
            UseDefaultCredentials = false,
        };
        HttpClient client = new HttpClient(handler);

        // This works when not behind a proxy
        //HttpClient client = new HttpClient();

        return client;
    }

    public async Task GetWeatherAsync(double longitude, double latitude, string username)
    {

        // MissingMethodException is thrown at this point
        var client = GetHttpClient();
        client.BaseAddress = new Uri(string.Format("http://api.geonames.org/findNearByWeatherJSON?lat={0}&lng={1}&username={2}", latitude, longitude, username));

        try
        {
            var response = await client.GetAsync(client.BaseAddress);
            if (response.IsSuccessStatusCode)
            {
                var JsonResult = response.Content.ReadAsStringAsync().Result;
                var weather = JsonConvert.DeserializeObject<WeatherResult>(JsonResult);

                SetValues(weather);
            }
            else
            {
                Debug.WriteLine(response.RequestMessage);
            }
        }
        catch (HttpRequestException ex)
        {
            Debug.WriteLine(ex.Message);
        }
        catch (System.Net.WebException ex)
        {
            Debug.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }

      

+2


source to share


1 answer


Add Microsoft.Net.Http

the NuGet package to your platform project. If you have a problem adding this, try installing the latest Microsoft.Bcl.Build

package first . Then after that add the HTTP package.



+3


source







All Articles