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

4.4k views Asked by At

This is a Xamarin solution and 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) and does so at the point where it invokes the method in the PCL which instantiates the HttpClient. Note that it doesn't even get to that method. The error appears on the application start-up method; e.g., UIApplication.Main()

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.

Further investigation showed that the device projects had no references to System.Net.Http. So I added these -- and it indicates Xamarin.iOS and Xamarin.Android as the packages -- but it still produces the error.

I'm not clear what the error is telling me but I believe it means that the device project can't see 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);
        }
    }
1

There are 1 answers

4
therealjohn On BEST ANSWER

Add the Microsoft.Net.Http NuGet package to your platform project too. If you run into an issue adding this, try installing the latest Microsoft.Bcl.Build package first. Then, after that is installed, add the HTTP package.