In a .NET Core Web Application I am using middleware (app.UseMyMiddleware) to add some logging on each request:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(MyMiddleware.GenericExceptionHandler);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseMyMiddleware();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public static void UseMyMiddleware(this IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
await Task.Run(() => HitDetails.StoreHitDetails(context));
await next.Invoke();
});
}
public static void StoreHitDetails(HttpContext context)
{
var config = (IConfiguration)context.RequestServices.GetService(typeof(IConfiguration));
var settings = new Settings(config);
var connectionString = config.GetConnectionString("Common");
var features = context.Features.Get<IHttpRequestFeature>();
var url = $"{features.Scheme}://{context.Request.Host.Value}{features.RawTarget}";
var parameters = new
{
SYSTEM_CODE = settings.SystemName,
REMOTE_HOST = context.Connection.RemoteIpAddress.ToString(),
HTTP_REFERER = context.Request.Headers["Referer"].ToString(),
HTTP_URL = url,
LOCAL_ADDR = context.Connection.LocalIpAddress.ToString(),
AUTH_USER = context.User.Identity.Name
};
using (IDbConnection db = new SqlConnection(connectionString))
{
db.Query("StoreHitDetails", parameters, commandType: CommandType.StoredProcedure);
}
}
This all works fine and I can grab most of what I need from the request but what I need next is the Form Data on a POST method.
context.Request.Form is an available option but when debugging I hover over it and see "The function evaluation requires all thread to run". If I try to use it the application just hangs.
What do I need to do to access Request.Form or is there an alternative property with POST data that I'm not seeing?
Task.Run
which will run yourStoreHitDetails
on a ThreadPool, and that's why you are seeing that message when you hover overRequest.Form
. Why don't you move all the logic in theStoreHitDetails
toUseMyMiddleware
? Or try usingRequest.Form
afterawait Task.Run(() => HitDetails.StoreHitDetails(context));
is finished. I hope that helps. – Favourite