Ejemplo n.º 1
0
        /// <summary>
        /// Serialize the error message to JSON / Xml
        /// </summary>
        /// <param name="ex">Exception to be deseralized</param>
        /// <returns>Deserialized error message.</returns>
        public static ErrorResponseEx DeserializeErrorMessage(Exception ex)
        {
            ErrorResponseEx errorResponse = null;
            string          errorMessage  = ex.InnerException.Message;

            if (!string.IsNullOrEmpty(errorMessage))
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(errorMessage)))
                {
                    errorResponse = (ErrorResponseEx)xmlSerializer.ReadObject(ms);
                }
            }

            return(errorResponse);
        }
        /// <summary>
        /// Method to handle binding redirection exception. This exception means that the 
        /// user's data is located in another data center. This exception's details returns
        /// several urls that may work in this case. At least one url is guaranteed to work
        /// So we need to get all the URLs and try them
        /// </summary>
        /// <param name="parsedException">The binding redirection exception we received</param>
        /// <param name="operation">The operation to try</param>
        private void HandleBindingRedirectionException(ErrorResponseEx parsedException, Action operation)
        {
            List<string> urls = new List<string>();

            // Go thru the error details name\value pair
            foreach (ErrorDetail ed in parsedException.Values)
            {
                // if the name is something like url1, url2 add it to the list of URLs
                if (ed.Item.StartsWith("url"))
                {
                    urls.Add(ed.Value);
                }
            }

            // Now try each URL
            foreach (string newUrl in urls)
            {
                // We permanantly change the dataservice to point to the new URL
                // as none of the operations will work on the current url
                dataService = new DirectoryDataServiceProxy(new Uri(newUrl));

                try
                {
                    // try the operation
                    operation();

                    // if the operation is successful, break out of the loop
                    // all the subsequent operations will go to the new URL
                    break;
                }
                catch (Exception)
                {
                    // nothing can be done, try next URL
                }
            }
        }