Raysefo

Raysefo

  • 1.5k
  • 284
  • 150.3k

web api DelegatingHandler question?

Feb 5 2019 8:46 AM
Hello,
 
I wonder how to insert request/response JSON messages into database in delegatinghandler?
 
any help would be great.
  1. public class LogRequestAndResponseHandler : DelegatingHandler  
  2. {  
  3.     protected override async Task<HttpResponseMessage> SendAsync(  
  4.         HttpRequestMessage request, CancellationToken cancellationToken)  
  5.     {  
  6.         // log request body  
  7.         string requestBody = await request.Content.ReadAsStringAsync();  
  8.         Trace.WriteLine(requestBody);  
  9.   
  10.         // let other handlers process the request  
  11.         var result = await base.SendAsync(request, cancellationToken);  
  12.   
  13.         if (result.Content != null)  
  14.         {  
  15.             // once response body is ready, log it  
  16.             var responseBody = await result.Content.ReadAsStringAsync();  
  17.             Trace.WriteLine(responseBody);  
  18.         }  
  19.   
  20.         return result;  
  21.     }  

 

Answers (4)