コード例 #1
0
        private IAsyncResult MakeServerCall <T>(HttpWebRequest request, Action <T, IGSPServiceResponse> action, bool lockClient, bool noErrorLog = false)
        {
            var requestUri = request.RequestUri.ToString();

            return(request.BeginGetResponse(result =>
            {
                try
                {
                    var req = result.AsyncState as HttpWebRequest;
                    if (req == null)
                    {
                        return;
                    }

                    var response = req.EndGetResponseNoException(result) as HttpWebResponse;

                    //Get the json payload from the response
                    string json = "";
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        using (var sr = new StreamReader(responseStream))
                            json = sr.ReadToEnd();
                    }

                    //Create a GSPServiceResponse object
                    var serviceResponse = new GSPServiceResponse(response, json);

                    // Show error dialog if error occured and no parameter is supplied (parameter-based errors is handled in UI context)
                    if (noErrorLog && serviceResponse.HasErrors)
                    {
                        //Failed to log to server. This could mean that we have a network failure.
                        //TODO: Try to log to isolatedstorage and then log back to server when network is available again
                        //TODO::Mathias
                        Debug.Assert(false, "Failed to log to server");
                    }
                    else if (serviceResponse.HasErrors && string.IsNullOrEmpty(serviceResponse.ApiErrorCode) && string.IsNullOrEmpty(serviceResponse.ApiErrorParamName))
                    {
                        result = LogToServer <T>(action, lockClient, result, serviceResponse.InnerException, requestUri);

                        if (lockClient)
                        {
                            ThreadHelper.ExecuteOnUI(() =>
                            {
                                try
                                {
                                    ShowDialog(Localization.Resources.ServiceErrorDialog_Title, serviceResponse.ErrorMessage, serviceResponse.StatusDescription, false, null);
                                    //Dialog.ShowDialog(Localization.Resources.ServiceErrorDialog_Title, serviceResponse.ErrorMessage, serviceResponse.StatusDescription, false, null);
                                }
                                catch (Exception innerEx)
                                {
                                    throw new Exception("Exception in anonymous callback #1, requestUrl: " + requestUri, innerEx);
                                }
                            });
                        }

                        return;
                    }
                    if (serviceResponse.HasErrors)
                    {
                        action.Invoke(default(T), serviceResponse);
                    }
                    else
                    {
                        action.Invoke(_json.DeserializeJson <T>(json), serviceResponse);
                    }
                }
                catch (Exception ex)
                {
                    ThreadHelper.ExecuteOnUI(() =>
                    {
                        try
                        {
                            var innerEx = ex.InnerException as WebException;
                            string localUri = string.Empty;
                            if (innerEx != null)
                            {
                                localUri = innerEx.Response.ResponseUri.LocalPath;
                            }
                            string finalErrorMessage = string.Empty;

                            if (!string.IsNullOrEmpty(localUri))
                            {
                                finalErrorMessage += localUri + "\n\n";
                            }

                            finalErrorMessage += ex.ToString();
                            ShowDialog(Localization.Resources.ServiceErrorDialog_Title, "Error when calling server API", finalErrorMessage, false, null);
                        }
                        catch (Exception innerEx)
                        {
                            throw new Exception("Exception in anonymous callback #2, requestUrl: " + requestUri, innerEx);
                        }
                        finally
                        {
                            if (noErrorLog == false)
                            {
                                LogToServer <T>(action, lockClient, result, ex, requestUri);
                            }
                        }
                    });
                    //System.Diagnostics.Debug.Assert(false);
                }
                finally
                {
                    if (lockClient)
                    {
                        ThreadHelper.ExecuteOnUI(() => GSPApplicationService.Current.AppState.DequeueBusy());
                    }
                }
            }, request));
        }