Skip to content

Commit

Permalink
Always call AfterRequestHandled in MainMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden committed Apr 30, 2024
1 parent 2b8a773 commit 1a7c876
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
18 changes: 11 additions & 7 deletions Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,20 @@ public void HandleRequest(ListenerContext context, Lazy<IDatabaseContext> databa
{
context.ResponseHeaders.Add("Cache-Control", "max-age=" + cacheSeconds.Value);
}

object? val = method.Invoke(group, invokeList.ToArray());
Response returnedResponse = GenerateResponseFromEndpoint(val, attribute, method);

foreach (Service service in this._services)
try
{
service.AfterRequestHandled(context, returnedResponse, method, database);
object? val = method.Invoke(group, invokeList.ToArray());
Response returnedResponse = GenerateResponseFromEndpoint(val, attribute, method);
return returnedResponse;
}
finally
{
foreach (Service service in this._services)
{
service.AfterRequestHandled(context, null, method, database);
}
}

return returnedResponse;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Bunkum.Core/Services/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override void Initialize()
}

/// <inheritdoc />
public override void AfterRequestHandled(ListenerContext context, Response response, MethodInfo method, Lazy<IDatabaseContext> database)
public override void AfterRequestHandled(ListenerContext context, Response? response, MethodInfo method, Lazy<IDatabaseContext> database)
{
this._tokenCache.Value = null;
}
Expand Down
7 changes: 4 additions & 3 deletions Bunkum.Core/Services/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ internal DebugService(Logger logger) : base(logger)
}

/// <inheritdoc/>
public override void AfterRequestHandled(ListenerContext context, Response response, MethodInfo method, Lazy<IDatabaseContext> database)
public override void AfterRequestHandled(ListenerContext context, Response? response, MethodInfo method, Lazy<IDatabaseContext> database)
{
if (!method.HasCustomAttribute<DebugResponseBodyAttribute>()) return;
if (response == null) return;

string body = Encoding.UTF8.GetString(response.Data);
this.Logger.LogDebug(BunkumCategory.Request, "Response body for {0} ({1} bytes):\n{2}", context.Uri.AbsolutePath, response.Data.Length, body);
string body = Encoding.UTF8.GetString(response.Value.Data);
this.Logger.LogDebug(BunkumCategory.Request, "Response body for {0} ({1} bytes):\n{2}", context.Uri.AbsolutePath, response.Value.Data.Length, body);
}
}
3 changes: 2 additions & 1 deletion Bunkum.Core/Services/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public virtual void Initialize()

/// <summary>
/// Runs after the request has been handled by the main middleware.
/// May also be called when a response fails to generate due to an exception. A null response is passed in this case.
/// </summary>
public virtual void AfterRequestHandled(ListenerContext context, Response response, MethodInfo method, Lazy<IDatabaseContext> database)
public virtual void AfterRequestHandled(ListenerContext context, Response? response, MethodInfo method, Lazy<IDatabaseContext> database)
{}

/// <summary>
Expand Down

0 comments on commit 1a7c876

Please sign in to comment.