mirror of
https://github.com/C9Glax/tranga.git
synced 2025-04-14 04:13:18 +02:00
Add Middleware to add Startofrequest time to all requests
This commit is contained in:
parent
892ef6c9d6
commit
90ce1395b8
35
API/HttpRequestTimeFeature.cs
Normal file
35
API/HttpRequestTimeFeature.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
namespace API;
|
||||||
|
|
||||||
|
public interface IHttpRequestTimeFeature
|
||||||
|
{
|
||||||
|
DateTime RequestTime { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HttpRequestTimeFeature : IHttpRequestTimeFeature
|
||||||
|
{
|
||||||
|
public DateTime RequestTime { get; }
|
||||||
|
|
||||||
|
public HttpRequestTimeFeature()
|
||||||
|
{
|
||||||
|
RequestTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RequestTimeMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
|
||||||
|
public RequestTimeMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task InvokeAsync(HttpContext context)
|
||||||
|
{
|
||||||
|
var httpRequestTimeFeature = new HttpRequestTimeFeature();
|
||||||
|
context.Features.Set<IHttpRequestTimeFeature>(httpRequestTimeFeature);
|
||||||
|
|
||||||
|
// Call the next delegate/middleware in the pipeline
|
||||||
|
return this._next(context);
|
||||||
|
}
|
||||||
|
}
|
@ -95,6 +95,8 @@ app.UseSwaggerUI(options =>
|
|||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
//app.UseMiddleware<RequestTimeMiddleware>();
|
||||||
|
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var db = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
|
var db = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user