public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails) { HttpStatusCode httpStatusCode = 0; string str = null; return(GatewayManagementHelper.TryGetExceptionDetails(exception, out errorDetails, out httpStatusCode, out str)); }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails) { HttpStatusCode httpStatusCode; string operationId; return(TryGetExceptionDetails(exception, out errorDetails, out httpStatusCode, out operationId)); }
/// <summary> /// is not found communication exception /// </summary> /// <param name="exception">Communication Exception</param> /// <returns>true if exception caused by resource not found, otherwise, false</returns> public static bool IsNotFoundCommunicationException(CommunicationException exception) { ServiceManagementError error = null; string operationId = string.Empty; ErrorHelper.TryGetExceptionDetails(exception, out error, out operationId); return(error != null && error.Code == HttpStatusCode.NotFound.ToString()); }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return(false); } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return(true); } var wex = exception.InnerException as WebException; if (wex == null) { return(false); } var response = wex.Response as HttpWebResponse; if (response == null) { return(false); } if (response.Headers != null) { operationId = response.Headers[WindowsAzure.ServiceManagement.Constants.OperationTrackingIdHeader]; } using (var s = response.GetResponseStream()) { try { if (s == null || s.Length == 0) { return(false); } var reader = XmlDictionaryReader.CreateTextReader(s, new XmlDictionaryReaderQuotas()); var ser = new DataContractSerializer(typeof(WindowsAzure.ServiceManagement.ServiceManagementError)); errorDetails = (ServiceManagementError)ser.ReadObject(reader, true); } catch (Exception) { return(false); } } return(true); }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return false; } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return true; } var wex = exception.InnerException as WebException; if (wex == null) { return false; } var response = wex.Response as HttpWebResponse; if (response == null) { return false; } if (response.Headers != null) { operationId = response.Headers[Constants.OperationTrackingIdHeader]; } using (var s = response.GetResponseStream()) { try { if (s == null || s.Length == 0) { return false; } var reader = XmlDictionaryReader.CreateTextReader(s, new XmlDictionaryReaderQuotas()); var ser = new DataContractSerializer(typeof(ServiceManagementError)); errorDetails = (ServiceManagementError)ser.ReadObject(reader, true); } catch (Exception) { return false; } } return true; }
protected override void WriteErrorDetails(CommunicationException exception) { ServiceManagementError serviceManagementError = null; GatewayManagementHelper.TryGetExceptionDetails(exception, out serviceManagementError); if (serviceManagementError != null) { object[] code = new object[2]; code[0] = serviceManagementError.Code; code[1] = serviceManagementError.Message; string str = string.Format(CultureInfo.InvariantCulture, "HTTP Status Code: {0} - HTTP Error Message: {1}", code); base.WriteError(new ErrorRecord(new CommunicationException(str), string.Empty, ErrorCategory.CloseError, null)); return; } else { base.WriteError(new ErrorRecord(exception, string.Empty, ErrorCategory.CloseError, null)); return; } }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return false; } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return false; } WebException wex = exception.InnerException as WebException; if (wex == null) { return false; } HttpWebResponse response = wex.Response as HttpWebResponse; if (response == null) { return false; } if (response.Headers != null) { operationId = response.Headers[Constants.OperationTrackingIdHeader]; } if (response.StatusCode == HttpStatusCode.NotFound) { errorDetails = new ServiceManagementError(); errorDetails.Message = response.ResponseUri.AbsoluteUri + " does not exist."; errorDetails.Code = response.StatusCode.ToString(); return false; } using (var s = response.GetResponseStream()) { if (s.Length == 0) { return false; } try { errorDetails = new ServiceManagementError(); s.Seek(0, SeekOrigin.Begin); var sr = new StreamReader(s); using (var err = new StringReader(sr.ReadToEnd())) { var reader = XmlReader.Create(err); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: { if (reader.Name == "Code") { reader.Read(); errorDetails.Code = reader.Value; } else if (reader.Name == "Message") { reader.Read(); errorDetails.Message = reader.Value; } break; } } } } } catch { errorDetails = null; return false; } } return true; }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out string operationId) { HttpStatusCode httpStatusCode; return TryGetExceptionDetails(exception, out errorDetails, out httpStatusCode, out operationId); }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return(false); } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return(true); } WebException wex = exception.InnerException as WebException; if (wex == null) { return(false); } HttpWebResponse response = wex.Response as HttpWebResponse; if (response == null) { return(false); } //httpStatusCode = response.StatusCode; //if (httpStatusCode == HttpStatusCode.Forbidden) //{ // return true; //} if (response.Headers != null) { operationId = response.Headers[Constants.OperationTrackingIdHeader]; } // Don't wrap responseStream in a using statement to prevent it // from being disposed twice (as it's disposed by reader if it is // successfully disposed). Stream responseStream = null; try { responseStream = response.GetResponseStream(); if (responseStream.Length == 0) { return(false); } try { using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(responseStream, new XmlDictionaryReaderQuotas())) { // Release the reference to responseStream since it // will be closed when the reader is diposed responseStream = null; DataContractSerializer ser = new DataContractSerializer(typeof(ServiceManagementError)); errorDetails = (ServiceManagementError)ser.ReadObject(reader, true); } } catch (SerializationException) { return(false); } } finally { if (responseStream != null) { responseStream.Dispose(); } } return(true); }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return(false); } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return(false); } WebException wex = exception.InnerException as WebException; if (wex == null) { return(false); } HttpWebResponse response = wex.Response as HttpWebResponse; if (response == null) { return(false); } if (response.Headers != null) { operationId = response.Headers[Constants.OperationTrackingIdHeader]; } if (response.StatusCode == HttpStatusCode.NotFound) { errorDetails = new ServiceManagementError(); errorDetails.Message = response.ResponseUri.AbsoluteUri + " does not exist."; errorDetails.Code = response.StatusCode.ToString(); return(false); } using (var s = response.GetResponseStream()) { if (s.Length == 0) { return(false); } try { errorDetails = new ServiceManagementError(); s.Seek(0, SeekOrigin.Begin); var sr = new StreamReader(s); using (var err = new StringReader(sr.ReadToEnd())) { var reader = XmlReader.Create(err); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: { if (reader.Name == "Code") { reader.Read(); errorDetails.Code = reader.Value; } else if (reader.Name == "Message") { reader.Read(); errorDetails.Message = reader.Value; } break; } } } } } catch { errorDetails = null; return(false); } } return(true); }
public void Run() { if (!ReadFromConfigFile() || !ValidateSubscriptionId() || !ValidateCertificate() || !this.Validate()) { Console.WriteLine("There was an error processing this command!"); return; } var serviceManagement = ServiceManagementHelper.CreateServiceManagementChannel("WindowsAzureEndPoint", CSManageCommand.Certificate); Console.WriteLine("Using certificate: " + CSManageCommand.Certificate.SubjectName.Name); try { string trackingId = null; HttpStatusCode?statusCode = null; string statusDescription = null; using (OperationContextScope scope = new OperationContextScope((IContextChannel)serviceManagement)) { try { this.PerformOperation(serviceManagement); if (WebOperationContext.Current.IncomingResponse != null) { trackingId = WebOperationContext.Current.IncomingResponse.Headers[Constants.OperationTrackingIdHeader]; statusCode = WebOperationContext.Current.IncomingResponse.StatusCode; statusDescription = WebOperationContext.Current.IncomingResponse.StatusDescription; Console.WriteLine("Operation ID: {0}", trackingId); } } catch (CommunicationException ce) { ServiceManagementError error = null; HttpStatusCode httpStatusCode = 0; string operationId; ServiceManagementHelper.TryGetExceptionDetails(ce, out error, out httpStatusCode, out operationId); if (error == null) { Console.WriteLine(ce.Message); } else { Console.WriteLine("HTTP Status Code: {0}", httpStatusCode); Console.WriteLine("Error Message: {0}", error.Message); Console.WriteLine("Operation Id: {0}", operationId); } } finally { if (statusCode != null) { Console.WriteLine("HTTP Status Code: {0}", statusCode); Console.WriteLine("StatusDescription: {0}", statusDescription); } } } if (trackingId != null && statusCode != null && statusCode == HttpStatusCode.Accepted) { Console.WriteLine("Waiting for async operation to complete:"); WaitForAsyncOperation(serviceManagement, trackingId); } } catch (TimeoutException) { Console.WriteLine("There was an error processing this command."); } }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { bool flag; errorDetails = null; httpStatusCode = 0; operationId = null; if (exception != null) { if (exception.Message != "Internal Server Error") { WebException innerException = exception.InnerException as WebException; if (innerException != null) { HttpWebResponse response = innerException.Response as HttpWebResponse; if (response != null) { if (response.Headers != null) { operationId = response.Headers["x-ms-request-id"]; } if (response.StatusCode != HttpStatusCode.NotFound) { Stream responseStream = response.GetResponseStream(); using (responseStream) { if (responseStream.Length != (long)0) { try { errorDetails = new ServiceManagementError(); responseStream.Seek((long)0, SeekOrigin.Begin); StreamReader streamReader = new StreamReader(responseStream); using (StringReader stringReader = new StringReader(streamReader.ReadToEnd())) { XmlReader xmlReader = XmlReader.Create(stringReader); while (xmlReader.Read()) { XmlNodeType nodeType = xmlReader.NodeType; if (nodeType != XmlNodeType.Element) { continue; } if (xmlReader.Name != "Code") { if (xmlReader.Name != "Message") { continue; } xmlReader.Read(); errorDetails.Message = xmlReader.Value; } else { xmlReader.Read(); errorDetails.Code = xmlReader.Value; } } } } catch (SerializationException serializationException) { flag = false; return(flag); } return(true); } else { flag = false; } } return(flag); } else { errorDetails = new ServiceManagementError(); errorDetails.Message = string.Concat(response.ResponseUri.AbsoluteUri, " does not exist."); errorDetails.Code = response.StatusCode.ToString(); return(false); } } else { return(false); } } else { return(false); } } else { httpStatusCode = HttpStatusCode.InternalServerError; return(true); } } else { return(false); } }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { bool flag; errorDetails = null; httpStatusCode = 0; operationId = null; if (exception != null) { if (exception.Message != "Internal Server Error") { WebException innerException = exception.InnerException as WebException; if (innerException != null) { HttpWebResponse response = innerException.Response as HttpWebResponse; if (response != null) { if (response.Headers != null) { operationId = response.Headers["x-ms-request-id"]; } Stream responseStream = response.GetResponseStream(); using (responseStream) { try { if (responseStream == null || responseStream.Length == (long)0) { flag = false; return(flag); } else { XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(responseStream, new XmlDictionaryReaderQuotas()); DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(ServiceManagementError)); errorDetails = (ServiceManagementError)dataContractSerializer.ReadObject(xmlDictionaryReader, true); } } catch (Exception exception1) { flag = false; return(flag); } return(true); } return(flag); } else { return(false); } } else { return(false); } } else { httpStatusCode = HttpStatusCode.InternalServerError; return(true); } } else { return(false); } }