Simplify async/await handling

This commit is contained in:
2022-07-16 13:29:55 +02:00
parent 63462fb870
commit 3d8dd8be31
5 changed files with 18 additions and 17 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Http;
using VAR.Json;
@@ -29,12 +30,12 @@ namespace VAR.WebFormsCore.Code
private static readonly Encoding Utf8Encoding = new UTF8Encoding();
public static void ResponseObject(this HttpContext context, object obj)
public static void ResponseObject(this HttpContext context, object obj, string contentType = "text/json")
{
context.Response.ContentType = "text/json";
context.Response.ContentType = contentType;
string strObject = JsonWriter.WriteObject(obj);
byte[] byteObject = Utf8Encoding.GetBytes(strObject);
context.Response.Body.WriteAsync(byteObject);
context.Response.Body.WriteAsync(byteObject).GetAwaiter().GetResult();
}
public static void SafeSet(this IHeaderDictionary header, string key, string value)

View File

@@ -10,7 +10,7 @@ namespace VAR.WebFormsCore.Code
{
#region Private methods
private static async Task ShowInternalErrorAsync(HttpContext context, Exception ex)
private static void ShowInternalError(HttpContext context, Exception ex)
{
try
{
@@ -36,8 +36,8 @@ namespace VAR.WebFormsCore.Code
sbOutput.Append("-->");
}
await context.Response.WriteAsync(sbOutput.ToString());
await context.Response.Body.FlushAsync();
context.Response.WriteAsync(sbOutput.ToString()).GetAwaiter().GetResult();
context.Response.Body.FlushAsync().GetAwaiter().GetResult();
}
catch
{
@@ -49,7 +49,7 @@ namespace VAR.WebFormsCore.Code
#region Public methods
public static async Task HandleErrorAsync(HttpContext context, Exception ex)
public static void HandleError(HttpContext context, Exception ex)
{
try
{
@@ -57,9 +57,9 @@ namespace VAR.WebFormsCore.Code
//context.Response.Clear();
//context.Handler = frmError;
frmError.ProcessRequest(context);
await context.Response.Body.FlushAsync();
context.Response.Body.FlushAsync().GetAwaiter().GetResult();
}
catch { await ShowInternalErrorAsync(context, ex); }
catch { ShowInternalError(context, ex); }
}
#endregion Public methods

View File

@@ -38,7 +38,7 @@ namespace VAR.WebFormsCore.Code
Console.WriteLine("!!!!!!!!!!");
Console.Write("Message: {0}\nStacktrace: {1}\n", ex.Message, ex.StackTrace);
await GlobalErrorHandler.HandleErrorAsync(httpContext, ex);
GlobalErrorHandler.HandleError(httpContext, ex);
}
}
}

View File

@@ -8,11 +8,11 @@ namespace VAR.WebFormsCore.Pages
{
#region IHttpHandler
public async void ProcessRequest(HttpContext context)
public void ProcessRequest(HttpContext context)
{
await context.Response.WriteAsync("<pre><code>");
await context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true));
await context.Response.WriteAsync("</code></pre>");
context.Response.WriteAsync("<pre><code>").GetAwaiter().GetResult();
context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true)).GetAwaiter().GetResult();
context.Response.WriteAsync("</code></pre>").GetAwaiter().GetResult();
}
#endregion IHttpHandler

View File

@@ -16,7 +16,7 @@ namespace VAR.WebFormsCore.Pages
private static readonly Encoding Utf8Encoding = new UTF8Encoding();
public async void ProcessRequest(HttpContext context)
public void ProcessRequest(HttpContext context)
{
try
{
@@ -58,7 +58,7 @@ namespace VAR.WebFormsCore.Pages
context.Response.Headers.SafeSet("Content-Type", "text/html");
byte[] byteObject = Utf8Encoding.GetBytes(stringWriter.ToString());
await context.Response.Body.WriteAsync(byteObject);
context.Response.Body.WriteAsync(byteObject).GetAwaiter().GetResult();
}
catch (Exception ex)
{
@@ -66,7 +66,7 @@ namespace VAR.WebFormsCore.Pages
Console.WriteLine("!!!!!!!!!!");
Console.Write("Message: {0}\nStacktrace: {1}\n", ex.Message, ex.StackTrace);
await GlobalErrorHandler.HandleErrorAsync(context, ex);
GlobalErrorHandler.HandleError(context, ex);
}
}