VAR.WebFormsCore: Use Async IO
This commit is contained in:
@@ -70,34 +70,27 @@ namespace VAR.WebFormsCore.Code
|
|||||||
|
|
||||||
#region Public methods
|
#region Public methods
|
||||||
|
|
||||||
public void WriteResponse(HttpResponse response, string contentType)
|
private static Encoding _utf8Econding = new UTF8Encoding();
|
||||||
|
|
||||||
|
public async void WriteResponse(HttpResponse response, string contentType)
|
||||||
{
|
{
|
||||||
|
StringWriter textWriter = new StringWriter();
|
||||||
response.ContentType = contentType;
|
response.ContentType = contentType;
|
||||||
foreach (string fileName in AssemblyFiles)
|
foreach (string fileName in AssemblyFiles)
|
||||||
{
|
{
|
||||||
Stream resourceStream = _assembly.GetManifestResourceStream(fileName);
|
Stream resourceStream = _assembly.GetManifestResourceStream(fileName);
|
||||||
string fileContent = new StreamReader(resourceStream).ReadToEnd();
|
string fileContent = new StreamReader(resourceStream).ReadToEnd();
|
||||||
byte[] byteArray = Encoding.UTF8.GetBytes(fileContent);
|
textWriter.Write(fileContent);
|
||||||
if (byteArray.Length > 0)
|
textWriter.Write("\n\n");
|
||||||
{
|
|
||||||
response.Body.Write(byteArray, 0, byteArray.Length);
|
|
||||||
|
|
||||||
byteArray = Encoding.UTF8.GetBytes("\n\n");
|
|
||||||
response.Body.Write(byteArray, 0, byteArray.Length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
foreach (string fileName in AbsoluteFiles)
|
foreach (string fileName in AbsoluteFiles)
|
||||||
{
|
{
|
||||||
string fileContent = File.ReadAllText(fileName);
|
string fileContent = File.ReadAllText(fileName);
|
||||||
byte[] byteArray = Encoding.UTF8.GetBytes(fileContent);
|
textWriter.Write(fileContent);
|
||||||
if (byteArray.Length > 0)
|
textWriter.Write("\n\n");
|
||||||
{
|
|
||||||
response.Body.Write(byteArray, 0, byteArray.Length);
|
|
||||||
|
|
||||||
byteArray = Encoding.UTF8.GetBytes("\n\n");
|
|
||||||
response.Body.Write(byteArray, 0, byteArray.Length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
byte[] byteObject = _utf8Econding.GetBytes(textWriter.ToString());
|
||||||
|
await response.Body.WriteAsync(byteObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Public methods
|
#endregion Public methods
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using VAR.Json;
|
using VAR.Json;
|
||||||
|
|
||||||
@@ -30,11 +31,14 @@ namespace VAR.WebFormsCore.Code
|
|||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Encoding _utf8Econding = new UTF8Encoding();
|
||||||
|
|
||||||
public static void ResponseObject(this HttpContext context, object obj)
|
public static void ResponseObject(this HttpContext context, object obj)
|
||||||
{
|
{
|
||||||
context.Response.ContentType = "text/json";
|
context.Response.ContentType = "text/json";
|
||||||
string strObject = JsonWriter.WriteObject(obj);
|
string strObject = JsonWriter.WriteObject(obj);
|
||||||
context.Response.WriteAsync(strObject);
|
byte[] byteObject = _utf8Econding.GetBytes(strObject);
|
||||||
|
context.Response.Body.WriteAsync(byteObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PrepareCacheableResponse(this HttpResponse response)
|
public static void PrepareCacheableResponse(this HttpResponse response)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace VAR.WebFormsCore.Code
|
|||||||
ServerHelpers.SetContentRoot(env.ContentRootPath);
|
ServerHelpers.SetContentRoot(env.ContentRootPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task Invoke(HttpContext httpContext)
|
public async Task Invoke(HttpContext httpContext)
|
||||||
{
|
{
|
||||||
httpContext.Response.Headers.Remove("Server");
|
httpContext.Response.Headers.Remove("Server");
|
||||||
httpContext.Response.Headers.Remove("X-Powered-By");
|
httpContext.Response.Headers.Remove("X-Powered-By");
|
||||||
@@ -36,16 +36,23 @@ namespace VAR.WebFormsCore.Code
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
if (ex is ThreadAbortException)
|
if (IsIgnoreException(ex) == false)
|
||||||
{
|
{
|
||||||
return null;
|
GlobalErrorHandler.HandleError(httpContext, ex);
|
||||||
}
|
}
|
||||||
GlobalErrorHandler.HandleError(httpContext, ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
await httpContext.Response.Body.FlushAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsIgnoreException(Exception ex)
|
||||||
|
{
|
||||||
|
if (ex is ThreadAbortException)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void RouteRequest(HttpContext context)
|
private void RouteRequest(HttpContext context)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ namespace VAR.WebFormsCore.Code
|
|||||||
{".7z", "application/x-7z-compressed"},
|
{".7z", "application/x-7z-compressed"},
|
||||||
};
|
};
|
||||||
|
|
||||||
public static void ResponseStaticFile(HttpContext context, string filePath)
|
public static async void ResponseStaticFile(HttpContext context, string filePath)
|
||||||
{
|
{
|
||||||
string extension = Path.GetExtension(filePath).ToLower();
|
string extension = Path.GetExtension(filePath).ToLower();
|
||||||
string contentType = null;
|
string contentType = null;
|
||||||
@@ -75,21 +75,14 @@ namespace VAR.WebFormsCore.Code
|
|||||||
contentType = _mimeTypeByExtension[extension];
|
contentType = _mimeTypeByExtension[extension];
|
||||||
}
|
}
|
||||||
|
|
||||||
//context.Response.Clear();
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(contentType) == false)
|
if (string.IsNullOrEmpty(contentType) == false)
|
||||||
{
|
{
|
||||||
context.Response.ContentType = contentType;
|
context.Response.ContentType = contentType;
|
||||||
}
|
}
|
||||||
context.Response.PrepareCacheableResponse();
|
context.Response.PrepareCacheableResponse();
|
||||||
|
|
||||||
//context.Response.Buffer = true;
|
|
||||||
//context.Response.WriteFile(filePath);
|
|
||||||
//context.Response.Flush();
|
|
||||||
//context.Response.Close();
|
|
||||||
//context.Response.End();
|
|
||||||
byte[] fileData = File.ReadAllBytes(filePath);
|
byte[] fileData = File.ReadAllBytes(filePath);
|
||||||
context.Response.Body.Write(fileData);
|
await context.Response.Body.WriteAsync(fileData);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using VAR.WebFormsCore.Code;
|
using VAR.WebFormsCore.Code;
|
||||||
using VAR.WebFormsCore.Controls;
|
using VAR.WebFormsCore.Controls;
|
||||||
@@ -12,7 +13,9 @@ namespace VAR.WebFormsCore.Pages
|
|||||||
|
|
||||||
public HttpContext Context { get; set; }
|
public HttpContext Context { get; set; }
|
||||||
|
|
||||||
public void ProcessRequest(HttpContext context)
|
private static Encoding _utf8Econding = new UTF8Encoding();
|
||||||
|
|
||||||
|
public async void ProcessRequest(HttpContext context)
|
||||||
{
|
{
|
||||||
StringWriter stringWriter = new();
|
StringWriter stringWriter = new();
|
||||||
|
|
||||||
@@ -54,7 +57,8 @@ namespace VAR.WebFormsCore.Pages
|
|||||||
if (context.Response.HasStarted) { return; }
|
if (context.Response.HasStarted) { return; }
|
||||||
|
|
||||||
context.Response.Headers.Add("Content-Type", "text/html");
|
context.Response.Headers.Add("Content-Type", "text/html");
|
||||||
Context.Response.WriteAsync(stringWriter.ToString());
|
byte[] byteObject = _utf8Econding.GetBytes(stringWriter.ToString());
|
||||||
|
await context.Response.Body.WriteAsync(byteObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool _isPostBack = false;
|
private bool _isPostBack = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user