protected void ButtonRun_Click(object sender, EventArgs e) { string errorMessage = null; try { DEAContext context = Session[_contextKey] as DEAContext; string jsonIn = context.ToJsonString(out errorMessage); if (string.IsNullOrEmpty(jsonIn)) { throw new Exception(errorMessage); } string serviceUrl = ConfigurationManager.AppSettings["deaServiceUrl"]; var client = new RestClient(); client.BaseUrl = new Uri(serviceUrl); RestRequest deaRequest = new RestRequest(); deaRequest.Method = Method.POST; deaRequest.AddParameter("text/plain", jsonIn, ParameterType.RequestBody); deaRequest.Resource = "json/DEA"; var response = client.Execute(deaRequest); if (response.IsSuccessful && response.ResponseStatus == ResponseStatus.Completed) { string jsonOut = response.Content; DEAResponse deaResponse = JsonConvert.DeserializeObject <DEAResponse>(jsonOut); if (deaResponse == null) { throw new Exception("Unable to de-serialize JSON response. Please check the log of the service."); } if (deaResponse.OK == false) { throw new Exception(deaResponse.errorMessage); } Session[_contextKey] = deaResponse.context; DEAEventArgs args = new DEAEventArgs(); args.Cargo = "DEAContextOK"; RaiseBubbleEvent(this, args); } else { if (!string.IsNullOrEmpty(response.ErrorMessage)) { throw new Exception(response.ErrorMessage); } else { throw new Exception(response.ResponseStatus.ToString()); } } } catch (Exception ex) { LabelError.Text = ex.Message; LabelError.Visible = true; DEAEventArgs args = new DEAEventArgs(); args.Cargo = "ERROR: " + ex.Message; RaiseBubbleEvent(this, args); } }
public Stream RunDEAnalysis(Stream request) { bool ok = true; string errorMessage = null; DEAContext context = null; string jsonRequest = null; string jsonResponse = null; const string fn = "RunDEAnalysis"; LOG.DebugFormat("{0} - started", fn); UriTemplateMatch utm = WebOperationContext.Current.IncomingRequest.UriTemplateMatch; LOG.Debug(utm.RequestUri.OriginalString); try { using (StreamReader reader = new StreamReader(request)) { jsonRequest = reader.ReadToEnd(); } LOG.DebugFormat("Request: {0}", jsonRequest); context = DEAContext.CreateFromJsonString(jsonRequest, out errorMessage); ok = (context != null); if (ok) { ok = context.RunDEA(out errorMessage); if (ok && context.ConstraintsSet) { ok = context.ApplyCostConstraintsToProjectSelection(out errorMessage); } if (ok) { jsonResponse = context.ToJsonString(out errorMessage); ok = (jsonResponse != null); } } } catch (Exception ex) { ok = false; errorMessage = ex.Message; } DEAResponse response = new DEAResponse(); response.OK = ok; response.errorMessage = errorMessage; response.context = context; jsonResponse = JsonConvert.SerializeObject(response, Formatting.Indented); LOG.DebugFormat("Response: {0}", jsonResponse); Stream ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(s: jsonResponse)); LOG.DebugFormat("{0} - ended", fn); return(ms); }