0

I have a very simple REST query to our WebAPI back end (used by a number of applications) and it works fine under Android and Windows but in iOS it fails with an "Object reference not set to an instance of an object" error. In the code below, both moHttpClient and loUri are instantiated. I've tried wrapping the call to GetEmployeeRecord in Device.BeginInvokeOnMainThread but that doesn't help either. I have upgraded to the latest stable version of Xamarin in Visual Studio and on my Mac. Why is it working in the other OSs but not iOS?

    private async void btnTest_Clicked(object sender, EventArgs e)
    {
        await GetEmployeeRecord();
    }

    private async Task GetEmployeeRecord()
    {
        try
        {
            var loUri = new Uri("https://my.website.com/mobile.webapp/api/employees?key=6c6f2c06-a444-4e54-bd77-b5f594c29910");
            var loResponse = await moHttpClient.GetAsync(loUri);
            if (loResponse.IsSuccessStatusCode)
            {
                var lcJson = await loResponse.Content.ReadAsStringAsync();
                await DisplayAlert("Employee", lcJson, "OK");
            }
        }
        catch (Exception ex)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                DisplayAlert("Get Employee Record", ex.Message, "OK");
            });
        }
    }

Using a breakpoint at the GetAsync line, both moHttpClient and loUri are instantiated. However, mousing over GetAsync gives a message that GetAsync is an unknown member. How can that be? Mouse over GetAsync

I just checked and it seems this Unknown member occurs with Android too. But with Android it actually executes.

12
  • I am getting 404 on URI you provided. What is moHttpClient ? Also you have celf-certs on your web site, that can be the problem too. If you want us to test please provide correct URI Commented Nov 26, 2016 at 3:20
  • That is not my web site, I just put that in to mask the actual address for security reasons. There is nothing wrong with the URI, it works with Android and Windows. I suspect I need to somehow set something in the iOS project to allow network/internet access. The code is in the PCL. Commented Nov 26, 2016 at 17:47
  • Then put breakpoint on your exception and check what object is null. I cannot help more if I cannot run it and see what is null Commented Nov 26, 2016 at 17:52
  • Sorry, I thought I mentioned before. It is the GetAsync that fails. Both moHttpClient and loUri are instantiated. During a break, mousing over GetAsync says the method does not exist. Commented Nov 26, 2016 at 22:00
  • 1
    Coincidentally I tried installing Microsoft.Net.Http before I came back here and saw your comment. I only installed it in the PCL because that is where the code is. IT WORKS!!! Now it is working so whatever comes standard with Xamarin Forms (or PCL base stuff) doesn't work for iOS. Commented Nov 29, 2016 at 22:49

1 Answer 1

3

In order to get the http query to work for iOS I had to install the NuGet package Microsoft.Net.Http in the PCL project, which is where the code is running.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.