System.MissingMethodException Method 'System.Net.Http.HttpClientHandler.set_Proxy' not found

[NOTE: If this is a duplicate post, my apologies. However, I could not find my original post from this morning no matter how I searched or looked for it.]

I am getting the error found in this message's title. Of course, I can easily confirm that there is a Proxy property on HttpClientHandler in the PCL project. And the solution builds without error. Only when I run does it produce this error (on either Droid or iOS) when it starts the app.

If I comment out the handler and instantiate HttpClient without a handler, it works fine as long as I'm on the open internet. But I'm trying to get this to work from behind a proxy.

After further investigation, I noted that neither device project had a reference to System.Net.Http. So I added that but it still produces the error.

MainPage.xaml.cs invokes the method:

        public async void OnClicked(object o, EventArgs e)
        {
            var longitude = double.Parse(Lon.Text);
            var latitude = double.Parse(Lat.Text);
            var username = User.Text;

            await vm.GetWeatherAsync(longitude, latitude, username);
        }

MainVM.cs instantiates the handler:

        public async Task GetWeatherAsync(double longitude, double latitude, string username)
        {
            var url = string.Format("http://api.geonames.org/findNearByWeatherJSON?lat={0}&lng={1}&username={2}", latitude, longitude, username);

            Uri proxyUri = new Uri(ProxyConfig.Url);
            NetworkCredential proxyCreds = new NetworkCredential(ProxyConfig.Username, ProxyConfig.Password);
            WebProxy proxy = new WebProxy(proxyUri)
            {
                Credentials = proxyCreds
            };

            HttpClientHandler handler = new HttpClientHandler
            {
                PreAuthenticate = true,
                UseDefaultCredentials = false,
                Proxy = proxy
            };

            HttpClient client = new HttpClient(handler);
            client.BaseAddress = new Uri(url);

            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);
            }
        }

Best Answers

Answers

Sign In or Register to comment.