监听WebAPI服务器所有用户请求
作者:C/S框架网  发布日期:2017/09/20 19:05:22
  监听WebAPI服务器所有用户请求

C# Code:

/// <summary>
/// 监听WebAPI服务器所有请求.
/// </summary>
public class RequestListenHandler : DelegatingHandler
{
   protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(
   HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
   {
      
      //控制台显示请求的URI信息
      Console.WriteLine("收到请求:" request.RequestUri);
      
      //调用WebAPI接口完成以后才执行下面的代码(在这里处理其他)
      return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
      {
         //添加日志
         Log2Server.AddRequestLog(request, task.Result as HttpResponseMessage);
         
         return task.Result;
         });
         
      }
   }
   
   //来源:C/S框架网(www.csframework.com) QQ:1980854898
   

C# Code:

config.MessageHandlers.Add(new RequestListenHandler());

//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇