Question

Using VS 2010, VB.NET, HTTPClient, .NET 4.0, and Windows Forms.

I am trying to get a windows application to consume JSON coming from a Web API that I have created. Web API works great and I can view the results from a browser. Found this article that I have been trying to get working only using VB.NET instead of C#. http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-wpf-application

The critical part of the code is this function:

private void GetProducts(object sender, RoutedEventArgs e)
{
btnGetProducts.IsEnabled = false;

client.GetAsync("api/products/2").ContinueWith((t) =>
{
    if (t.IsFaulted)
    {
        MessageBox.Show(t.Exception.Message);
        btnGetProducts.IsEnabled = true;
    }
    else
    {
        var response = t.Result;
        if (response.IsSuccessStatusCode)
        {
            response.Content.ReadAsAsync<IEnumerable<Product>>().
                ContinueWith(t2 =>
                    {
                        if (t2.IsFaulted)
                        {
                            MessageBox.Show(t2.Exception.Message);
                            btnGetProducts.IsEnabled = true;
                        }
                        else
                        {
                            var products = t2.Result;
                            _products.CopyFrom(products);
                            btnGetProducts.IsEnabled = true;
                        }
                    }, TaskScheduler.FromCurrentSynchronizationContext());
        }

    }
}, TaskScheduler.FromCurrentSynchronizationContext());
}

I have tried to convert this to VB.NET but I am having issues with the t.Result saying "'Result' is not a member of 'System.Threading.Tasks.Task'."

Here is my attempt to convert it to VB.NET:

Private Sub GetProducts(sender As Object, e As RoutedEventArgs)
    btnGetProducts.IsEnabled = False

    client.GetAsync("api/products/2") _
        .ContinueWith(Of HttpResponseMessage) _
        (Function(t)
             If t.IsFaulted Then
                 MessageBox.Show(t.Exception.Message)
                 btnGetProducts.IsEnabled = True
             Else

                 '***************************************************************
                 Dim response = t.Result 'This is the line that is giving me grief. Error Msg: 'Result' is not a member of 'System.Threading.Tasks.Task'.
                 '***************************************************************

                 If response.IsSuccessStatusCode Then
                     response.Content.ReadAsAsync(Of IEnumerable(Of SendNotice)).ContinueWith _
                         (Function(t2)
                              If t2.IsFaulted Then
                                  MessageBox.Show(t2.Exception.Message)
                                  btnGetProducts.IsEnabled = True
                              Else
                                  Dim products = t2.Result
                                  _lstSN.CopyFrom(products)
                                  btnGetProducts.IsEnabled = True
                              End If

                          End Function, TaskScheduler.FromCurrentSynchronizationContext())

                 End If
             End If

         End Function, TaskScheduler.FromCurrentSynchronizationContext())
End Sub

Any idea why I am getting this error and what am I missing in my code to allow me to catch the returning JSON data?

Thanks!

Was it helpful?

Solution

This is because VB.NET type inference is not great in Visual Studio 2010. You'll need to give the compiler a bit of extra help by specifying the actual type returned by your client.GetAsync(), like so:

client _
   .GetAsync("api/products/2") _
   .ContinueWith( _
       Sub(t As Task(Of HttpResponseMessage))
           ...
       End Sub, _
       TaskScheduler.FromCurrentSynchronizationContext())

Note: I've changed your Function to Sub because I didn't see any Return statements.

OTHER TIPS

Try this:

client.GetAsync("api/products/2") _
        .ContinueWith(Sub(t)
                       If t.IsFaulted Then
                       .
                       .
                       .

Try changing your lambda from a Function to a Sub.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top