Building on a previous question, I'm Intercepting incoming requests inside a middleware
of Configure
method.
public class Startup: IStartup
{
public Startup(IHostingEnvironment environment)
{
// starting apparatus
}
public void Configure(IApplicationBuilder app)
{
var self = this;
app.Run(async (context) =>
{
await self.Delegate.Service(context);
});
}
public void output(HttpContext context string output)
{
context.Response.WriteAsync(output);
}
// other methods
public IRouter Delegate { get; set; }
}
Handler
public class Handler: IRouter
{
public void Service(HttpContext context)
{
// shuttles the context to the other parts of the system
// for processing data
}
public void ServiceResult(HttpContext context, object result)
{
// system calls this method, which is then passed back to Startup object.
Startup.output(context, result);
}
}
Startup should hand over the context to the Handler for processing the request.
Handler then dispatches to another part of the system where request is processed based on the URI. that other part will invoke the ServerResult once processing is done.
Basically the workflow is
Startup
delegates context to the apparatus and apparatus returns the context along with result for output. there's a disconnect, output will happen in another function/closure/lambda but not the same middleware.
I'm blocked at this middleware where it's forcing me to await on this and define it as async, any further advice on how can I implement the above in asp.net core world.
P.S. Request interception can happen anywhere, it doesn't have to me in the middleware as in my example.
P.P.S An idea could be Startup can pass a lambda to the apparatus which apparatus can then call once request is finished processing.
My Attempt:
Startup
public void Configure(IApplicationBuilder app)
{
var self = this;
app.Run(async (context) =>
{
await self.Handler.Handle(new Request(context, new Task(() => { })));
});
}
public void Result(Request request)
{
request.Context.Response.WriteAsync(request.Result); // write the result
request.Task.Start(); // end waiting for the middleware async
}
Handler
public Task Handle(Request request)
{
// delegates to helper objects to process the request
return request.Task;
}
public void Result(Request request)
{
// helper objects called this function after populating results
startup.Result(request);
}