0

我正在从 WP8 应用程序调用本地 asp.net web api 方法,并得到异常:

asyncResult.AsyncWaitHandle' 引发了“System.NotSupportedException”类型的异常

有人能帮忙吗?

  public void GetProducts()
    {
        UriBuilder fullUri = new UriBuilder(baseUri + "productapi");
        fullUri.Query = "";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUri.Uri);
        request.Accept = "application/json";
        RequestUpdateState requestState = new RequestUpdateState();
        requestState.AsyncRequest = request;

        request.BeginGetResponse(new AsyncCallback(HandleProductsResponse),
            requestState);
    }

    private void HandleProductsResponse(IAsyncResult asyncResult)
    {
        // get the state information
        RequestUpdateState requestState = (RequestUpdateState)asyncResult.AsyncState;
        HttpWebRequest request = (HttpWebRequest)requestState.AsyncRequest;

        // end the async request
        requestState.AsyncResponse = (HttpWebResponse)request.EndGetResponse(asyncResult);

        Stream streamResult;        
        // get the stream containing the response from the async call
        streamResult = requestState.AsyncResponse.GetResponseStream();
        ObservableCollection<Product> productList = new ObservableCollection<Product>();

    }
4

1 回答 1

0

我的错,我还是以不同的方式做到了。

   public async void GetProducts()
    {
        UriBuilder fullUri = new UriBuilder(baseUri + "productapi");
        fullUri.Query = "";

        HttpClient c = new HttpClient();
        c.BaseAddress = fullUri.Uri;
        var response = await c.GetAsync(c.BaseAddress, HttpCompletionOption.ResponseContentRead);
        if (response.IsSuccessStatusCode)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Product));
            String str = await response.Content.ReadAsStringAsync();

            dynamic[] Objects = JsonConvert.DeserializeObject<Object[]>(str);
            Products.Clear();
            foreach (dynamic ob in Objects)
            {
              //do something
            }              
        }
    }

感谢您的帮助

于 2013-08-20T13:56:15.450 回答