Program.cs:
using OldHandlers;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddSystemWebAdapters(); // Register SystemWebAdapters
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// Register SystemWebAdapters middleware
app.UseSystemWebAdapters();
// Define endpoints
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/myhandler", async context =>
{
var adapter = new AspNetCoreHttpContextAdapter(context);
var handler = new MyHandler();
handler.ProcessRequest(adapter);
});
});
app.UseAuthorization();
app.MapRazorPages();
app.Run();
And I have:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SystemWebAdapters;
using System.Threading.Tasks;
using OldHandlers;
public class AspNetCoreHttpContextAdapter : IHttpContextAdapter
{
private readonly HttpContext _context;
public AspNetCoreHttpContextAdapter(HttpContext context)
{
_context = context;
}
public string ResponseContentType
{
get => _context.Response.ContentType;
set => _context.Response.ContentType = value;
}
public void WriteResponse(string content)
{
_context.Response.WriteAsync(content);
}
public async Task ReadRequestBodyAsync()
{
using (StreamReader reader = new StreamReader(_context.Request.Body))
{
return await reader.ReadToEndAsync();
}
}
public void SetStatusCode(int statusCode)
{
_context.Response.StatusCode = statusCode;
}
public object GetRouteData(string key)
{
return _context.Items[key];
}
}
In my old website, I created a new handler:
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var adapter = new SystemWebHttpContextAdapter(new HttpContextWrapper(context));
var handler = new MyHandler();
handler.ProcessRequest(adapter);
}
public bool IsReusable => false;
}
and:
using System.IO;
using System.Threading.Tasks;
using System.Web;
using OldHandlers;
public class SystemWebHttpContextAdapter : IHttpContextAdapter
{
private readonly HttpContextBase _context;
public SystemWebHttpContextAdapter(HttpContextBase context)
{
_context = context;
}
public string ResponseContentType
{
get => _context.Response.ContentType;
set => _context.Response.ContentType = value;
}
public void WriteResponse(string content)
{
_context.Response.Write(content);
}
public async Task ReadRequestBodyAsync()
{
using (StreamReader reader = new StreamReader(_context.Request.InputStream))
{
return await reader.ReadToEndAsync();
}
}
public void SetStatusCode(int statusCode)
{
_context.Response.StatusCode = statusCode;
}
public object GetRouteData(string key)
{
return _context.Items[key];
}
}
I also created a .NET Standard 2.0 library to contain the concrete handler implementation:
namespace OldHandlers
{
public class MyHandler
{
public void ProcessRequest(IHttpContextAdapter context)
{
context.ResponseContentType = "text/plain";
context.WriteResponse("Hello, World!");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace OldHandlers
{
public interface IHttpContextAdapter
{
string ResponseContentType { get; set; }
void WriteResponse(string content);
Task ReadRequestBodyAsync();
void SetStatusCode(int statusCode);
object GetRouteData(string key);
}
}
What I don't understand is that, if I use Microsoft.AspNetCore.SystemWebAdapters, then do I need to create my adapter classes, AspNetCoreHttpContextAdapter and SystemWebHttpContextAdapter?
0 comments:
Post a Comment
Thanks