ApiGuidGeneratorBehaviour.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using AipGateway.API.Application.Modules;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.Extensions.Logging;
  4. namespace AipGateway.API.Application.Pipeline.Middlewares.Behaviours
  5. {
  6. public class ApiGuidGeneratorBehaviour
  7. {
  8. private readonly ILogger<ApiGuidGeneratorBehaviour> _log;
  9. private readonly RequestDelegate _next;
  10. public ApiGuidGeneratorBehaviour(ILogger<ApiGuidGeneratorBehaviour> logger, RequestDelegate next)
  11. {
  12. _log = logger;
  13. _next = next;
  14. _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiGuidGeneratorMiddleware");
  15. }
  16. public async Task InvokeAsync(HttpContext context)
  17. {
  18. _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiGuidGeneratorMiddleware, {0}, {1}",
  19. context.Connection.RemoteIpAddress, context.Request.Path);
  20. string requestUrl = context.Request.Path;
  21. if (requestUrl.Contains(GlobalConstants.API_ENDPOINT))
  22. {
  23. string guid = Guid.NewGuid().ToString();
  24. //context.Items.Add(GlobalConstants.API_GUID, guid);
  25. context.Items[GlobalConstants.API_GUID] = guid;
  26. }
  27. await _next(context);
  28. await Task.FromResult(Task.CompletedTask);
  29. return;
  30. }
  31. }
  32. }