/// <summary> /// Parse the Response For Deployments And Return Them /// </summary> /// <param name="httpWebResponse">The Response From the Web Service</param> /// <returns>List of Deployments</returns> private static ServiceDashboardResponse ParseResponseFromJson(HttpWebResponse httpWebResponse) { //try //{ if (httpWebResponse.StatusCode == HttpStatusCode.OK) { //if (httpWebResponse.ContentType==) DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ServiceDashboardResponse)); if (httpWebResponse.ContentLength > int.MaxValue) { throw new IndexOutOfRangeException(String.Format("Response From Web Service Exceeds {0}", int.MaxValue)); } // WWB: Read The Data From The Response Stream int length = (int)httpWebResponse.ContentLength; byte[] data = new byte[length]; using (Stream responseStream = httpWebResponse.GetResponseStream()) { responseStream.Read(data, 0, length); } // WWB: In A Memory Stream Convert the Json using (MemoryStream stream = new MemoryStream(data)) { var response = serializer.ReadObject(stream) as ServiceDashboardResponse; // WWB: Check The Response For Errors if (response.Status != ServiceDashboardResponseStatus.Success) { var exception = new WebServiceException<ServiceDashboardResponseStatus>() { Status = response.Status }; throw exception; } return response; } } else { ServiceDashboardResponse ErrorResponse = new ServiceDashboardResponse() { Status = ServiceDashboardResponseStatus.IllegalResponse, Dashboard = null }; return ErrorResponse; } //} //catch (Exception ex) //{ // String message = ex.Message; // //if (ex is SerializationException) // //{ // // return ServiceDashboardResponse ErrorResponse = new ServiceDashboardResponse() // // { // // Status = ServiceDashboardResponse.IllegalResponse, //Dashboard = null, //ServiceCode = String.Empty // // }; // //} // throw new Exception(); //} }
/// <summary> /// Callback from Request For RSS /// </summary> /// <param name="asynchronousResult">Aync Response From Request</param> private static void HttpWebResponseServiceDashboardCallback(IAsyncResult asynchronousResult) { // State of request is asynchronous. InternalRequestState<ServiceDashboardRequest> requestState = (InternalRequestState<ServiceDashboardRequest>)asynchronousResult.AsyncState; // WWB: Stop The Time Out Timer -- Request Has Completed requestState.HttpWebRequestTimer.SafeStop(); try { HttpWebRequest httpWebRequest = requestState.HttpWebRequest; using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asynchronousResult)) { if (httpWebReponse.ContentType.Contains("application/json")) { ServiceDashboardResponse response = ParseResponseFromJson(httpWebReponse); ((ServiceDashboardRequest)requestState.ExternalRequestState).Dashboard = response.Dashboard; requestState.Callback(new WebServiceResponse<ServiceDashboardRequest>(requestState.ExternalRequestState)); } else //assume error { ServiceDashboardResponse ErrorResponse = new ServiceDashboardResponse() { Status = ServiceDashboardResponseStatus.IllegalResponse, Dashboard=null }; } } } catch (Exception exception) { // WWB: All Exception Are Handled By The Exception Callback if (requestState.ExceptionCallback == null) { throw; } else { // WWB: Package Up A Exception Response Object ExceptionReponse<ServiceDashboardRequest> response = new ExceptionReponse<ServiceDashboardRequest>() { Exception = exception, Request = requestState.ExternalRequestState }; // Call The Exception Call Back With the Exception Response To Report Exceptions To The Caller requestState.ExceptionCallback(new WebServiceResponse<ExceptionReponse<ServiceDashboardRequest>>(response)); } } }