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;
using System.IO;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using VAR.Json; using VAR.Json;
@@ -29,12 +30,12 @@ namespace VAR.WebFormsCore.Code
private static readonly Encoding Utf8Encoding = new UTF8Encoding(); 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); string strObject = JsonWriter.WriteObject(obj);
byte[] byteObject = Utf8Encoding.GetBytes(strObject); 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) public static void SafeSet(this IHeaderDictionary header, string key, string value)

View File

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

View File

@@ -38,7 +38,7 @@ namespace VAR.WebFormsCore.Code
Console.WriteLine("!!!!!!!!!!"); Console.WriteLine("!!!!!!!!!!");
Console.Write("Message: {0}\nStacktrace: {1}\n", ex.Message, ex.StackTrace); 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 #region IHttpHandler
public async void ProcessRequest(HttpContext context) public void ProcessRequest(HttpContext context)
{ {
await context.Response.WriteAsync("<pre><code>"); context.Response.WriteAsync("<pre><code>").GetAwaiter().GetResult();
await context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true)); context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true)).GetAwaiter().GetResult();
await context.Response.WriteAsync("</code></pre>"); context.Response.WriteAsync("</code></pre>").GetAwaiter().GetResult();
} }
#endregion IHttpHandler #endregion IHttpHandler

View File

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