static async Task WriteResultsToStream(AdomdDataReader results, Stream stream, CancellationToken cancel, ILogger log) { if (results == null) { log.LogInformation("Null results"); return; } using var rdr = results; //can't call Dispose on these without syncronous IO on the underlying connection var tw = new StreamWriter(stream, encoding, 1024 * 4, true); var w = new Newtonsoft.Json.JsonTextWriter(tw); int rows = 0; try { await w.WriteStartArrayAsync(cancel); while (rdr.Read()) { if (cancel.IsCancellationRequested) { throw new TaskCanceledException(); } rows++; await w.WriteStartObjectAsync(cancel); for (int i = 0; i < rdr.FieldCount; i++) { string name = rdr.GetName(i); object value = rdr.GetValue(i); await w.WritePropertyNameAsync(name, cancel); await w.WriteValueAsync(value, cancel); } await w.WriteEndObjectAsync(cancel); if (rows % 50000 == 0) { log.LogInformation($"Wrote {rows} rows to output stream."); } } log.LogInformation($"Finished Writing {rows} rows to output stream."); await w.WriteEndArrayAsync(cancel); await w.FlushAsync(); await tw.FlushAsync(); await stream.FlushAsync(); } catch (TaskCanceledException ex) { log.LogWarning($"Writing results canceled after {rows} rows."); } }
public static async Task WriteResultsToStream(object results, Stream stream, CancellationToken cancel) { if (results == null) { return; } if (results is AdomdDataReader rdr) { var encoding = new System.Text.UTF8Encoding(false); using (var tw = new StreamWriter(stream, encoding, 1024 * 4, true)) using (var w = new Newtonsoft.Json.JsonTextWriter(tw)) { await w.WriteStartObjectAsync(cancel); var rn = "rows"; await w.WritePropertyNameAsync(rn); await w.WriteStartArrayAsync(cancel); while (rdr.Read()) { await w.WriteStartObjectAsync(cancel); for (int i = 0; i < rdr.FieldCount; i++) { string name = rdr.GetName(i); object value = rdr.GetValue(i); await w.WritePropertyNameAsync(name, cancel); await w.WriteValueAsync(value, cancel); } await w.WriteEndObjectAsync(cancel); } await w.WriteEndArrayAsync(cancel); await w.WriteEndObjectAsync(cancel); await w.FlushAsync(); await tw.FlushAsync(); await stream.FlushAsync(); } } else if (results is CellSet cs) { throw new NotSupportedException("CellSet results"); } else { throw new InvalidOperationException("Unexpected result type"); } }
async System.Threading.Tasks.Task Microsoft.AspNetCore.Mvc.IActionResult.ExecuteResultAsync( Microsoft.AspNetCore.Mvc.ActionContext context) { if (context == null) { throw new System.ArgumentNullException("context"); } if (JsonRequestBehavior == JsonRequestBehavior_t.DenyGet && string.Equals(context.HttpContext.Request.Method, "GET", System.StringComparison.OrdinalIgnoreCase)) { throw new System.InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."); } Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response; // https://stackoverflow.com/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean response.ContentType = this.ContentType + "; charset=" + this.ContentEncoding.WebName; if (Data == null) { using (System.IO.StreamWriter writer = new System.IO.StreamWriter(response.Body, this.ContentEncoding)) { // await writer.WriteLineAsync("null"); await writer.WriteLineAsync("{}"); } // End Using writer return; } // End if (Data == null) #if false Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; using (System.IO.StreamWriter writer = new System.IO.StreamWriter(response.Body, this.ContentEncoding)) { using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(writer)) { Newtonsoft.Json.JsonSerializer ser = Newtonsoft.Json.JsonSerializer.Create(jsonSerializerSettings); ser.Serialize(jsonWriter, Data); await jsonWriter.FlushAsync(); } // End Using jsonWriter await writer.FlushAsync(); } // End Using writer #endif System.Text.Json.JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions() { IncludeFields = true, WriteIndented = true, PropertyNamingPolicy = this.NamingPolicy // PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase }; await System.Text.Json.JsonSerializer.SerializeAsync(response.Body, Data, options); } // End Task ExecuteResultAsync
public async Task <IActionResult> Upload(IEnumerable <IFormFile> files, [FromForm] string metaData) { if (User.Identity.IsAuthenticated == false) { return(BadRequest(new Models.ApiErrorResult("User must be authenticated to view measures."))); } if (!Request.ContentType.Contains("multipart/form-data")) { return(BadRequest(new Models.ApiErrorResult("Content must be mime multipart."))); } if (!User.Claims.Any(cl => cl.Type == Identity.Claims.SubmitMeasure_Key)) { return(BadRequest(new Models.ApiErrorResult("The user does not have permission to submit measures."))); } ChunkMetaData metadata = Newtonsoft.Json.JsonConvert.DeserializeObject <ChunkMetaData>(Request.Form["metadata"]); if (!metadata.FileExtension.EndsWith("xlsx", StringComparison.OrdinalIgnoreCase) && !metadata.FileExtension.EndsWith("json", StringComparison.OrdinalIgnoreCase)) { return(BadRequest(new Models.ApiErrorResult("Only Excel and json files are valid."))); } var user = await _modelDB.Users.FindAsync(Guid.Parse(User.Claims.Where(x => x.Type == ClaimTypes.NameIdentifier).Select(x => x.Value).FirstOrDefault())); await _fileService.WriteToStreamAsync(files.FirstOrDefault(), metadata); if (!metadata.IsFinalChunck) { return(Ok(new UploadResult(metadata.UploadUid, metadata.IsFinalChunck))); } List <string> errors = new List <string>(); string metricName = null; Guid? metricID = null; try { DQM.Models.MeasureSubmissionViewModel measure = null; if (metadata.FileExtension.EndsWith("xlsx", StringComparison.OrdinalIgnoreCase)) { using (var stream = _fileService.ReturnTempFileStream(metadata.UploadUid)) using (var document = SpreadsheetDocument.Open(stream, false)) { var reader = new ASPE.DQM.Utils.MeasuresExcelReader(document); measure = reader.Convert(errors); document.Close(); } //Can delete the excel file regardless of validation, will be saved as json if successfull await _fileService.DeleteTempFileChunkAsync(metadata.UploadUid); if (errors.Count > 0) { return(BadRequest(new UploadResult(metadata.UploadUid, true, errors.ToArray()))); } //validate the submission. if (ValidateSubmission(measure, errors) == false) { return(BadRequest(new UploadResult(metadata.UploadUid, true, errors.ToArray()))); } //save as json if valid using (var ms = new System.IO.MemoryStream()) { using (var sw = new System.IO.StreamWriter(ms, System.Text.Encoding.UTF8, 1024, true)) using (var jw = new Newtonsoft.Json.JsonTextWriter(sw)) { var serializerSettings = new Newtonsoft.Json.JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.None, DateFormatString = "'yyyy-MM-dd'" }; var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.DateFormatString = "yyyy'-'MM'-'dd"; serializer.Formatting = Newtonsoft.Json.Formatting.None; serializer.Serialize(jw, measure); await jw.FlushAsync(); } ms.Seek(0, System.IO.SeekOrigin.Begin); //ms.Position = 0; await _fileService.WriteToStreamAsync(metadata.UploadUid, 0, ms); } } else { //assume json file using (var stream = _fileService.ReturnTempFileStream(metadata.UploadUid)) using (var sr = new System.IO.StreamReader(stream)) using (var jr = new Newtonsoft.Json.JsonTextReader(sr)) { var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.DateFormatString = "yyyy'-'MM'-'dd"; measure = serializer.Deserialize <Models.MeasureSubmissionViewModel>(jr); } //validate the submission. if (ValidateSubmission(measure, errors) == false) { //upload is invalid, delete the temp file await _fileService.DeleteTempFileChunkAsync(metadata.UploadUid); return(BadRequest(new UploadResult(metadata.UploadUid, true, errors.ToArray()))); } } metricName = await _modelDB.Metrics.Where(m => m.ID == measure.MetricID.Value).Select(m => m.Title).FirstOrDefaultAsync(); metricID = measure.MetricID; }catch (Exception ex) { _logger.LogError(ex, "Error validating pending measure upload."); errors.Add(ex.Message); } if (errors.Count > 0) { return(BadRequest(new UploadResult(metadata.UploadUid, true, errors.ToArray()) { metricID = metricID, metricName = metricName })); } return(Ok(new UploadResult(metadata.UploadUid, true, metricID, metricName))); }
} // End Sub WriteArray public static async System.Threading.Tasks.Task AnyDataReaderToJson( System.Data.Common.DbConnection cnn , string sql , RenderType_t format , Microsoft.AspNetCore.Http.HttpContext context , System.Text.Encoding encoding , object parameters = null , System.Data.IDbTransaction transaction = null , int?commandTimeout = null , System.Data.CommandType?commandType = null ) { // DynamicParameters dbArgs = new DynamicParameters(); using (System.IO.StreamWriter responseWriter = new System.IO.StreamWriter(context.Response.Body, encoding)) { using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(responseWriter)) { if (format.HasFlag(RenderType_t.Indented)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; } try { using (System.Data.Common.DbDataReader dr = await cnn.ExecuteDbReaderAsync(sql, parameters, transaction, commandTimeout, commandType)) { context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK; context.Response.ContentType = "application/json; charset=" + encoding.WebName; await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("tables"); await jsonWriter.WriteStartArrayAsync(); do { if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("columns"); if (format.HasFlag(RenderType_t.Columns_Associative)) { await WriteAssociativeColumnsArray(jsonWriter, dr, format); } else if (format.HasFlag(RenderType_t.Columns_ObjectArray)) { await WriteComplexArray(jsonWriter, dr, format); } else // (format.HasFlag(RenderType_t.Array)) { await WriteArray(jsonWriter, dr); } } // End if (!format.HasFlag(RenderType_t.Data_Only)) if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync("rows"); } // End if (!format.HasFlag(RenderType_t.Data_Only)) await jsonWriter.WriteStartArrayAsync(); string[] columns = null; if (format.HasFlag(RenderType_t.DataTable)) { columns = new string[dr.FieldCount]; for (int i = 0; i < dr.FieldCount; i++) { columns[i] = dr.GetName(i); } // Next i } // End if (format.HasFlag(RenderType_t.DataTable)) while (await dr.ReadAsync()) { if (format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); } else { await jsonWriter.WriteStartArrayAsync(); } for (int i = 0; i <= dr.FieldCount - 1; i++) { object obj = await dr.GetFieldValueAsync <object>(i); if (obj == System.DBNull.Value) { obj = null; } if (columns != null && format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync(columns[i]); } await jsonWriter.WriteValueAsync(obj); } // Next i if (format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } else { await jsonWriter.WriteEndArrayAsync(); } } // Whend await jsonWriter.WriteEndArrayAsync(); if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } // End if (!format.HasFlag(RenderType_t.Data_Only)) await jsonWriter.FlushAsync(); await responseWriter.FlushAsync(); } while (await dr.NextResultAsync()); await jsonWriter.WriteEndArrayAsync(); await jsonWriter.WriteEndObjectAsync(); await jsonWriter.FlushAsync(); await responseWriter.FlushAsync(); } // End Using dr } catch (System.Exception ex) { context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError; context.Response.ContentType = "application/json; charset=" + encoding.WebName; await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("Error"); await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("Message"); await jsonWriter.WriteValueAsync(ex.Message); await jsonWriter.WritePropertyNameAsync("StackTrace"); await jsonWriter.WriteValueAsync(ex.StackTrace); await jsonWriter.WriteEndObjectAsync(); await jsonWriter.WriteEndObjectAsync(); } await jsonWriter.FlushAsync(); await responseWriter.FlushAsync(); await context.Response.CompleteAsync(); } // End Using jsonWriter } // End Using responseWriter } // End Sub AnyDataReaderToJson
} // End Task DeserializeJSON private static async System.Threading.Tasks.Task Ransack( Microsoft.AspNetCore.Http.HttpContext context , System.Net.WebSockets.WebSocket webSocket) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); SearchArguments searchArguments = await DeserializeJSON <SearchArguments>(webSocket, serializer); string[] fieldNames = LinqHelper.GetFieldAndPropertyNames <SearchResult>(); Getter_t <SearchResult>[] getters = LinqHelper.GetGetters <SearchResult>(fieldNames); using (WebSocketTextWriter wtw = new WebSocketTextWriter(webSocket)) { #if true using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(wtw)) { System.Threading.Tasks.Task wsa = jsonWriter.WriteStartArrayAsync(); // jsonWriter.WriteStartArray(); int j = 0; foreach (SearchResult thisSearchResult in FileSearch.SearchContent2(searchArguments)) { await wsa; // serializer.Serialize(jsonWriter, thisSearchResult); // jsonWriter.WriteStartObject(); await jsonWriter.WriteStartObjectAsync(); // jsonWriter.WritePropertyName("CharPos"); // jsonWriter.WriteValue(thisSearchResult.CharPos); // jsonWriter.WritePropertyName("File"); // jsonWriter.WriteValue(thisSearchResult.File); // jsonWriter.WritePropertyName("Line"); // jsonWriter.WriteValue(thisSearchResult.Line); // jsonWriter.WritePropertyName("LineNumber"); // jsonWriter.WriteValue(thisSearchResult.LineNumber); // jsonWriter.WritePropertyName("SearchTerm"); // jsonWriter.WriteValue(thisSearchResult.SearchTerm); /* * * await jsonWriter.WritePropertyNameAsync("CharPos"); * await jsonWriter.WriteValueAsync(thisSearchResult.CharPos); * * await jsonWriter.WritePropertyNameAsync("CharPos"); * await jsonWriter.WriteValueAsync(thisSearchResult.CharPos); * * await jsonWriter.WritePropertyNameAsync("File"); * await jsonWriter.WriteValueAsync(thisSearchResult.File); * * await jsonWriter.WritePropertyNameAsync("Line"); * await jsonWriter.WriteValueAsync(thisSearchResult.Line); * * await jsonWriter.WritePropertyNameAsync("LineNumber"); * await jsonWriter.WriteValueAsync(thisSearchResult.LineNumber); * * await jsonWriter.WritePropertyNameAsync("SearchTerm"); * await jsonWriter.WriteValueAsync(thisSearchResult.SearchTerm); */ for (int i = 0; i < getters.Length; ++i) { System.Threading.Tasks.Task wpnt = jsonWriter.WritePropertyNameAsync(fieldNames[i]); object value = getters[i](thisSearchResult); // if (value == System.DBNull.Value) value = null; await wpnt; await jsonWriter.WriteValueAsync(value); } // Next i // await awso; // jsonWriter.WriteEndObject(); System.Threading.Tasks.Task weo = jsonWriter.WriteEndObjectAsync(); // await weo; if (j > 0 && j % 200 == 0) { j++; await weo; await jsonWriter.WriteEndArrayAsync(); await jsonWriter.FlushAsync(); // await wtw.FlushAsync(); await wtw.SendAsync(true); await jsonWriter.WriteStartArrayAsync(); } // Next j else { j++; await weo; } } // Next thisSearchResult await jsonWriter.WriteEndArrayAsync(); //jsonWriter.WriteEndArray(); await jsonWriter.FlushAsync(); // jsonWriter.Flush(); } // End Using jsonWriter #else // System.Collections.Generic.List<SearchResult> ls = FileSearch.SearchContent(searchArguments); System.Collections.Generic.IEnumerable <SearchResult> ls = FileSearch.SearchContent2(searchArguments); using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(wtw)) { serializer.Serialize(jsonWriter, ls); await jsonWriter.FlushAsync(); } // End Using jsonWriter #endif await wtw.SendAsync(true); } // End Using wtw await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "Normal closure; the connection successfully completed whatever purpose for which it was created.", System.Threading.CancellationToken.None); serializer = null; } // End Task Ransack
} // End Sub WriteArray public static async System.Threading.Tasks.Task AnyDataReaderToJson( string sql , System.Collections.Generic.Dictionary<string, object> pars , Microsoft.AspNetCore.Http.HttpContext context , RenderType_t format) { SqlService service = (SqlService) context.RequestServices.GetService(typeof(SqlService)); using (System.Data.Common.DbConnection con = service.Connection) { using (System.Data.Common.DbCommand cmd = con.CreateCommand()) { cmd.CommandText = sql; // cmd.CommandText = "SELECT * FROM T_Benutzer; SELECT * FROM T_Benutzergruppen;"; service.AddParameterList(pars, cmd); // cmd.ExecuteNonQueryAsync // cmd.ExecuteReaderAsync // cmd.ExecuteScalarAsync using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync( System.Data.CommandBehavior.SequentialAccess | System.Data.CommandBehavior.CloseConnection)) { using (System.IO.StreamWriter output = new System.IO.StreamWriter(context.Response.Body)) { using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output) { if (format.HasFlag(RenderType_t.Indented)) jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; context.Response.StatusCode = (int) System.Net.HttpStatusCode.OK; context.Response.ContentType = "application/json"; await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("tables"); await jsonWriter.WriteStartArrayAsync(); do { if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("columns"); if (format.HasFlag(RenderType_t.Columns_Associative)) { await WriteAssociativeColumnsArray(jsonWriter, dr, format); } else if (format.HasFlag(RenderType_t.Columns_ObjectArray)) { await WriteComplexArray(jsonWriter, dr, format); } else // (format.HasFlag(RenderType_t.Array)) { await WriteArray(jsonWriter, dr); } } // End if (!format.HasFlag(RenderType_t.Data_Only)) if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync("rows"); } // End if (!format.HasFlag(RenderType_t.Data_Only)) await jsonWriter.WriteStartArrayAsync(); string[] columns = null; if (format.HasFlag(RenderType_t.DataTable)) { columns = new string[dr.FieldCount]; for (int i = 0; i < dr.FieldCount; i++) { columns[i] = dr.GetName(i); } // Next i } // End if (format.HasFlag(RenderType_t.DataTable)) while (await dr.ReadAsync()) { if (format.HasFlag(RenderType_t.DataTable)) await jsonWriter.WriteStartObjectAsync(); else await jsonWriter.WriteStartArrayAsync(); for (int i = 0; i <= dr.FieldCount - 1; i++) { object obj = await dr.GetFieldValueAsync<object>(i); if (obj == System.DBNull.Value) obj = null; if (columns != null && format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync(columns[i]); } await jsonWriter.WriteValueAsync(obj); } // Next i if (format.HasFlag(RenderType_t.DataTable)) await jsonWriter.WriteEndObjectAsync(); else await jsonWriter.WriteEndArrayAsync(); } // Whend await jsonWriter.WriteEndArrayAsync(); if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } // End if (!format.HasFlag(RenderType_t.Data_Only)) } while (await dr.NextResultAsync()); await jsonWriter.WriteEndArrayAsync(); await jsonWriter.WriteEndObjectAsync(); await jsonWriter.FlushAsync(); await output.FlushAsync(); } // jsonWriter } // output } // dr } // End Using cmd if (con.State != System.Data.ConnectionState.Closed) con.Close(); } // con } // End Sub WriteArray
} // End Sub WriteArray public static async System.Threading.Tasks.Task <System.Exception> AsJSON( this IDbConnection cnn , System.IO.TextWriter output , string sql , RenderType_t format , object param = null , IDbTransaction transaction = null , int?commandTimeout = null , CommandType?commandType = null) { try { using (System.Data.Common.DbDataReader dr = cnn.ExecuteDbReader(sql, param, transaction, commandTimeout, commandType)) { using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output) { if (format.HasFlag(RenderType_t.Indented)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; } // https://stackoverflow.com/questions/15305203/what-to-do-with-errors-when-streaming-the-body-of-an-http-request // https://tools.ietf.org/html/rfc2616#section-14.40 // https://stackoverflow.com/questions/50299300/how-to-write-malformed-http-response-to-guarantee-something-akin-to-http-500 // https://stackoverflow.com/questions/50298999/write-http-trailer-headers-manually // How do I write a useful trail header to the response that can be displayed well by the browser? // You don’t. Mainstream Web browsers do not care about trailers. // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK; // context.Response.ContentType = "application/json"; await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("tables"); await jsonWriter.WriteStartArrayAsync(); do { if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("columns"); if (format.HasFlag(RenderType_t.Columns_Associative)) { await WriteAssociativeColumnsArray(jsonWriter, dr, format); } else if (format.HasFlag(RenderType_t.Columns_ObjectArray)) { await WriteComplexArray(jsonWriter, dr, format); } else // (format.HasFlag(RenderType_t.Array)) { await WriteArray(jsonWriter, dr); } } // End if (!format.HasFlag(RenderType_t.Data_Only)) if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync("rows"); } // End if (!format.HasFlag(RenderType_t.Data_Only)) await jsonWriter.WriteStartArrayAsync(); string[] columns = null; if (format.HasFlag(RenderType_t.DataTable)) { columns = new string[dr.FieldCount]; for (int i = 0; i < dr.FieldCount; i++) { columns[i] = dr.GetName(i); } // Next i } // End if (format.HasFlag(RenderType_t.DataTable)) while (await dr.ReadAsync()) { if (format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); } else { await jsonWriter.WriteStartArrayAsync(); } for (int i = 0; i <= dr.FieldCount - 1; i++) { object obj = await dr.GetFieldValueAsync <object>(i); if (obj == System.DBNull.Value) { obj = null; } if (columns != null && format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync(columns[i]); } await jsonWriter.WriteValueAsync(obj); } // Next i if (format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } else { await jsonWriter.WriteEndArrayAsync(); } } // Whend await jsonWriter.WriteEndArrayAsync(); if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } // End if (!format.HasFlag(RenderType_t.Data_Only)) } while (await dr.NextResultAsync()); await jsonWriter.WriteEndArrayAsync(); await jsonWriter.WriteEndObjectAsync(); await jsonWriter.FlushAsync(); await output.FlushAsync(); } // jsonWriter } // End Using dr } // End Try catch (System.Exception ex) { return(ex); } // End Catch return(null); } // End Task AsJSON
} // End Sub WriteArray public static async System.Threading.Tasks.Task AnyDataReaderToJson( string sql , System.Collections.Generic.Dictionary <string, object> pars , SqlService service , WebSocketTextWriter output , RenderType_t format) { using (System.Data.Common.DbConnection con = service.Connection) { using (System.Data.Common.DbCommand cmd = con.CreateCommand()) { cmd.CommandText = sql; service.AddParameterList(pars, cmd); // cmd.ExecuteNonQueryAsync // cmd.ExecuteReaderAsync // cmd.ExecuteScalarAsync using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync( System.Data.CommandBehavior.SequentialAccess | System.Data.CommandBehavior.CloseConnection)) { using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output) { if (format.HasFlag(RenderType_t.Indented)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; } // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK; // context.Response.ContentType = "application/json"; await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("tables"); await jsonWriter.WriteStartArrayAsync(); do { if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync("columns"); if (format.HasFlag(RenderType_t.Columns_Associative)) { await WriteAssociativeColumnsArray(jsonWriter, dr, format); } else if (format.HasFlag(RenderType_t.Columns_ObjectArray)) { await WriteComplexArray(jsonWriter, dr, format); } else // (format.HasFlag(RenderType_t.Array)) { await WriteArray(jsonWriter, dr); } } // End if (!format.HasFlag(RenderType_t.Data_Only)) if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync("rows"); } // End if (!format.HasFlag(RenderType_t.Data_Only)) await output.TransmitAsync(); await jsonWriter.WriteStartArrayAsync(); if (dr.HasRows) { string[] columns = null; if (format.HasFlag(RenderType_t.DataTable)) { columns = new string[dr.FieldCount]; for (int i = 0; i < dr.FieldCount; i++) { columns[i] = dr.GetName(i); } // Next i } // End if (format.HasFlag(RenderType_t.DataTable)) int rowCount = 0; while (await dr.ReadAsync()) { if (format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteStartObjectAsync(); } else { await jsonWriter.WriteStartArrayAsync(); } for (int i = 0; i <= dr.FieldCount - 1; i++) { object obj = await dr.GetFieldValueAsync <object>(i); if (obj == System.DBNull.Value) { obj = null; } if (columns != null && format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WritePropertyNameAsync(columns[i]); } await jsonWriter.WriteValueAsync(obj); } // Next i if (format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } else { await jsonWriter.WriteEndArrayAsync(); } rowCount++; await jsonWriter.FlushAsync(); if (rowCount % 5 == 0) { await output.TransmitAsync(); } else { await output.FlushAsync(); } } // Whend await jsonWriter.FlushAsync(); await output.TransmitAsync(); } // End if (dr.HasRows) await jsonWriter.WriteEndArrayAsync(); if (!format.HasFlag(RenderType_t.Data_Only) && !format.HasFlag(RenderType_t.DataTable)) { await jsonWriter.WriteEndObjectAsync(); } // End if (!format.HasFlag(RenderType_t.Data_Only)) } while (await dr.NextResultAsync()); await jsonWriter.WriteEndArrayAsync(); await jsonWriter.WriteEndObjectAsync(); try { await jsonWriter.FlushAsync(); await output.TransmitAsync(); } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } } // jsonWriter } // dr } // End Using cmd if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } // con } // End Sub WriteArray
public async Task TestUploadingXLSXAndConvert() { var app = new ApplicationFactory((IServiceCollection services) => { services.Add(new ServiceDescriptor(typeof(Files.IFileService), typeof(Files.AzureBlobStorageFileService), ServiceLifetime.Transient)); }); using (var scope = app.CreateScope()) { string sampleFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\Measures\HPHCI"); string[] files = Directory.GetFiles(sampleFolder); var service = (Files.IFileService)scope.ServiceProvider.GetService(typeof(Files.IFileService)); foreach (var file in files) { string identifier = Guid.NewGuid().ToString("D"); Console.WriteLine("Reading file: " + file); using (var fs = new FileStream(file, FileMode.Open)) { await service.WriteToStreamAsync(identifier, 0, fs); } List <string> errors = new List <string>(); try { DQM.Models.MeasureSubmissionViewModel measure = null; using (var stream = service.ReturnTempFileStream(identifier)) using (var sr = new System.IO.StreamReader(stream)) using (var document = SpreadsheetDocument.Open(stream, false)) { var reader = new ASPE.DQM.Utils.MeasuresExcelReader(document); measure = reader.Convert(errors); document.Close(); } //Can delete the excel file regardless of validation, will be saved as json if successfull await service.DeleteTempFileChunkAsync(identifier); //save as json if valid using (var ms = new System.IO.MemoryStream()) { using (var sw = new System.IO.StreamWriter(ms, System.Text.Encoding.UTF8, 1024, true)) using (var jw = new Newtonsoft.Json.JsonTextWriter(sw)) { var serializerSettings = new Newtonsoft.Json.JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.None, DateFormatString = "'yyyy-MM-dd'" }; var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.DateFormatString = "yyyy'-'MM'-'dd"; serializer.Formatting = Newtonsoft.Json.Formatting.None; serializer.Serialize(jw, measure); await jw.FlushAsync(); } ms.Seek(0, System.IO.SeekOrigin.Begin); //ms.Position = 0; await service.WriteToStreamAsync(identifier, 0, ms); } } catch (Exception ex) { Console.WriteLine($"Failed on file {file}, with error: {ex.Message}"); await service.DeleteTempFileChunkAsync(identifier); //Assert.Fail(); } try { Models.MeasureSubmissionViewModel import; using (var stream = service.ReturnTempFileStream(identifier)) using (var sr = new System.IO.StreamReader(stream)) using (var jr = new Newtonsoft.Json.JsonTextReader(sr)) { var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.DateFormatString = "yyyy'-'MM'-'dd"; import = serializer.Deserialize <Models.MeasureSubmissionViewModel>(jr); } await service.DeleteTempFileChunkAsync(identifier); } catch (Exception ex) { Console.WriteLine($"Failed on file {file}, with error: {ex.Message}"); await service.DeleteTempFileChunkAsync(identifier); //Assert.Fail(); } } } }
public async Task UploadJsonMeasure() { var app = new ApplicationFactory(); using (var scope = app.CreateScope()) using (var db = scope.ServiceProvider.GetRequiredService <Model.ModelDataContext>()) { //get the first count metric var metric = db.Metrics.Include(m => m.ResultsType).Where(m => m.ResultsType.Value == "Count" && m.Statuses.OrderByDescending(s => s.CreateOn).First().MetricStatusID == Model.MetricStatus.PublishedID).FirstOrDefault(); if (metric == null) { Assert.Fail("There is no published metric to create measures for."); } var rnd = new Random(); var measureMeta = new Models.MeasureSubmissionViewModel { MetricID = metric.ID, ResultsType = metric.ResultsType.Value, DataSource = "Unit Test DataSource", Organization = "Unit Test Organization", Network = "Developerland", RunDate = DateTime.Now.Date, DateRangeStart = DateTime.Now.AddYears(Convert.ToInt32(-100 * rnd.NextDouble())).Date, DateRangeEnd = DateTime.Now.AddMonths(Convert.ToInt32(-12 * rnd.NextDouble())).Date, SupportingResources = "https://github.com/some-organization/repositoryurl" }; List <Models.MeasurementSubmissionViewModel> measurements = new List <Models.MeasurementSubmissionViewModel>(); int totalRows = Convert.ToInt32(20 * rnd.NextDouble()) + 1; for (int i = 0; i < totalRows; i++) { measurements.Add(new Models.MeasurementSubmissionViewModel { RawValue = i.ToString(), Definition = i.ToString(), Measure = Convert.ToSingle(100 * rnd.NextDouble()) }); } float sum = measurements.Sum(m => m.Measure.Value); foreach (var measure in measurements) { measure.Total = sum; } measureMeta.Measures = measurements; using (var ms = new System.IO.MemoryStream()) { using (var sw = new System.IO.StreamWriter(ms, Encoding.Default, 1024, true)) using (var jw = new Newtonsoft.Json.JsonTextWriter(sw)) { var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.DateFormatString = "yyyy'-'MM'-'dd"; serializer.Formatting = Newtonsoft.Json.Formatting.None; serializer.Serialize(jw, measureMeta); await jw.FlushAsync(); } ms.Seek(0, SeekOrigin.Begin); using (var http = new System.Net.Http.HttpClient()) { http.DefaultRequestHeaders.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.Default.GetBytes("dqm1:Password1!"))); try { var content = new System.Net.Http.StreamContent(ms); content.Headers.Add("Content-Type", "application/json"); var result = await http.PostAsync("https://localhost:44317/api/measures/submit", content); if (result.IsSuccessStatusCode) { var responseContent = await result.Content.ReadAsStringAsync(); } else { string error = await result.Content.ReadAsStringAsync(); Assert.Fail(error); } } catch (System.Net.WebException webex) { using (var reader = new StreamReader(webex.Response.GetResponseStream())) { Assert.Fail(await reader.ReadToEndAsync()); } } } } } }