public void DisposeAsync_ClosesWriterAndLeavesBuilderAvailable() { var sb = new StringBuilder(); var sw = new StringWriter(sb); sw.Write("hello"); Assert.True(sw.DisposeAsync().IsCompletedSuccessfully); Assert.True(sw.DisposeAsync().IsCompletedSuccessfully); Assert.Throws <ObjectDisposedException>(() => sw.Write(42)); Assert.Equal("hello", sb.ToString()); }
private static RequestDelegate GetHandler<T, TE>( Dictionary<string,List<AuthorizeAttribute>> restrictions, Dictionary<Type, EntityKeyDescribtion> entityKeys, DatabaseTypes dbType, string connString, InteractingType interactingType, bool allowAnonimus, string authentificationPath, string accessDeniedPath, Dictionary<string,RequestParamName> _requestParams, JsonSerializerOptions jsonSerializerOptions = null, Func<T> customDbContextFactory = null ) where T : DbContext, IDisposable where TE : class { return async (context) => { bool authResult = Authorization<TE>(context, HttpMethod.Get, restrictions, allowAnonimus,authentificationPath,accessDeniedPath); if (!authResult) { return; } var e = entityKeys; var QueryParams = RequestParams.RetriveQueryParam(context.Request.Query, _requestParams); IEnumerable<TE> queryResult; using (T dbcontext = CreateContext<T>(connString, dbType, customDbContextFactory)) { queryResult = GetDBQueryResult<T, TE>(dbcontext, QueryParams); } if (interactingType == InteractingType.JSON) { byte[] jsonUtf8Bytes; jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(queryResult, jsonSerializerOptions); await context.Response.WriteAsync(System.Text.Encoding.UTF8.GetString(jsonUtf8Bytes)); } else if (interactingType == InteractingType.XML) { XmlRootAttribute a = new XmlRootAttribute("result"); // XmlSerializer does not support IEnumerable<T> XmlSerializer serializer = new XmlSerializer(typeof(List<TE>),a); StringWriter textWriter = new StringWriter(); serializer.Serialize(textWriter,queryResult.ToList()); await context.Response.WriteAsync(textWriter.ToString()); await textWriter.DisposeAsync(); } }; }
public async Task <string> ConvertAsync(ConverterType converterType, string[] input) { if (input.Length == 0) { return(string.Empty); } using StringWriter allContent = new StringWriter(); allContent.WriteClassStudioHeader(); for (int i = 0; i < input.Length; ++i) { await this.ConvertAsync(converterType, input[i], false, allContent); await allContent.WriteLineAsync(); } await allContent.DisposeAsync(); return(allContent.ToString()); }
private static RequestDelegate PostHandler<T, TE>( bool update, Dictionary<string,List<AuthorizeAttribute>> restrictions, DatabaseTypes dbType, string connString, InteractingType interactingType, string authentificationPath, string accessDeniedPath, JsonSerializerOptions jsonSerializerOptions = null, MethodInfo dbContextBeforeSaveChangesMethod = null, Func<T> customDbContextFactory = null ) where T : DbContext, IDisposable where TE : class { return async (context) => { bool authResult = Authorization<TE>(context, HttpMethod.Post, restrictions, false,authentificationPath,accessDeniedPath); if (!authResult) { return; } TE recivedData; List<TE> recivedDataList; var mi = GetActionBeforeSave<TE>(); string Reason = ""; using (T dbcontext = CreateContext<T>(connString, dbType, customDbContextFactory)) { if (interactingType == InteractingType.JSON) { using (var reader = new StreamReader(context.Request.Body)) { var body = await reader.ReadToEndAsync(); try { recivedData = JsonSerializer.Deserialize<TE>(body); if (recivedData != null) { if (CheckAllowed<T, TE>(dbcontext, recivedData, mi, out Reason)) { if (!update) { dbcontext.Set<TE>().Add(recivedData); } else { dbcontext.Set<TE>().Update(recivedData); } DoBeforeContextSaveChanges<T>(dbContextBeforeSaveChangesMethod, dbcontext); await dbcontext.SaveChangesAsync(); byte[] jsonUtf8Bytes; jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(recivedData, jsonSerializerOptions); await context.Response.WriteAsync(System.Text.Encoding.UTF8.GetString(jsonUtf8Bytes)); } else { await context.Response.WriteAsync(Reason); } } } catch { try { recivedDataList = JsonSerializer.Deserialize<List<TE>>(body); if (recivedDataList != null) { if (!CheckAllowedList<T, TE>(dbcontext, recivedDataList, mi, out Reason)) { await context.Response.WriteAsync(Reason); } else { if (!update) { dbcontext.Set<TE>().AddRange(recivedDataList); } else { dbcontext.Set<TE>().UpdateRange(recivedDataList); } DoBeforeContextSaveChanges<T>(dbContextBeforeSaveChangesMethod, dbcontext); await dbcontext.SaveChangesAsync(); byte[] jsonUtf8Bytes; jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(recivedDataList, jsonSerializerOptions); await context.Response.WriteAsync(System.Text.Encoding.UTF8.GetString(jsonUtf8Bytes)); } } } catch { } } } } else if (interactingType == InteractingType.XML) { using (var reader = new StreamReader(context.Request.Body)) { var body = await reader.ReadToEndAsync(); Stream stream = GenerateStreamFromString(body); XmlRootAttribute a = new XmlRootAttribute("result"); XmlSerializer serializer = new XmlSerializer(typeof(List<TE>),a); var recivedDataL = (List<TE>)serializer.Deserialize(stream); await stream.DisposeAsync(); if (recivedDataL != null && recivedDataL.Count > 0) { if (CheckAllowedList<T, TE>(dbcontext, recivedDataL, mi, out Reason)) { if (!update) { dbcontext.Set<TE>().AddRange(recivedDataL); } else { dbcontext.Set<TE>().UpdateRange(recivedDataL); } DoBeforeContextSaveChanges<T>(dbContextBeforeSaveChangesMethod, dbcontext); await dbcontext.SaveChangesAsync(); StringWriter textWriter = new StringWriter(); serializer.Serialize(textWriter, recivedDataL.ToList()); await context.Response.WriteAsync(textWriter.ToString()); await textWriter.DisposeAsync(); } else { await context.Response.WriteAsync(Reason); } } // Do something } } } }; }