Forum Xamarin Cross Platform with Xamarin

System.MissingMethodException: Method 'System.Net.Http.HttpClientHandler.set_Proxy'

Something very strange going on which I don't understand. At bottom is the full error stack. How could the HttpClientHandler not have a set_Proxy method? HttpClientHandler has a documented Proxy property. And this error only happens at runtime which, if indeed it didn't have a set_Proxy method that it would be caught at compile time, right?

The project code works fine when outside of a proxy and when I comment out the HttpClientHandler code and instantiate the HttpClient without a handler using new HttpClient(). However, I'm altering the project to work inside a proxy and when adding the handler, it gives this error at runtime. The error appears at the point where it instantiates executes the GetWeatherAsync(longitude, latitude, username) method.

MainPage.xaml.cs has the OnClicked event handler:

public partial class MainPage : ContentPage
{
    MainVM vm;

    public MainPage()
    {
        vm = new MainVM();
        BindingContext = vm;

        InitializeComponent();
    }

    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 has the GetWeatherAsync method:

    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(<url_to_proxy_pac>);
        NetworkCredential proxyCreds = new NetworkCredential(<username>, <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);
        }
    }

This is the stack trace from the error:

System.MissingMethodException: Method 'System.Net.Http.HttpClientHandler.set_Proxy' not found.
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00031] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:316
  at REST_demo.MainVM.GetWeatherAsync (System.Double longitude, System.Double latitude, System.String username) [0x00027] in <dab20c90349241ec967fe0fe72a53057>:0
  at REST_demo.MainPage+<OnClicked>c__async0.MoveNext () [0x0006e] in /Users/barnesr/Source/Git/xamarin-sample/REST_demo/REST_demo/Views/MainPage.xaml.cs:24
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018
  at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/3511/ce955cc0/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:18
  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/3511/ce955cc0/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/3511/ce955cc0/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Java.Lang.IRunnable.cs:81
  at at (wrapper dynamic-method) System.Object:310b26ad-20a4-4a24-9f3c-babd2e55e4dd (intptr,intptr)
Sign In or Register to comment.