wcf - Web API 2.0 - 如何验证 ASP.NET Web API 中的传入请求,类似于 WCF AfterReceiveRequest(IDispatchMessageInspector 成员)

标签 wcf asp.net-mvc-5 asp.net-web-api2

我正在将现有的 WCF 服务迁移到 Web API。在 WCF 中,实现了 IDispatchMessageInspector 成员,用于验证/记录传入请求和传出回复。就像下面这样

“#region IDispatchMessageInspector 成员”

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        //Log the Request with Log4Net
        Logging.InfoMessage = string.Format("{0} - {1}", "Before calling ", GetWebMethodName());
        Logging.DebugMessage = request.ToString();
        Logging.WriteLog();

        //Logic to validate Request
        }
        return null;
    }        

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        //Log the Response with Log4Net
        Logging.DebugMessage = reply.ToString();
        Logging.InfoMessage = string.Format("{0} - {1}", "After called ", GetWebMethodName());
        Logging.WriteLog();
    }

#endregion

我想在 webAPI 2.0 中实现同样的目标。执行此操作后,所有请求验证以及日志记录都会在全局范围内处理。

如果您已经这样做过,请分享您的经验..

最佳答案

我建议创建一个自定义消息处理程序。自定义消息处理程序是一个继承自DelegatingHandler类的类。

所有进入 WebAPI 请求处理周期的请求首先由请求处理程序处理,所有发出的响应最后由处理程序处理。

https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf

关于wcf - Web API 2.0 - 如何验证 ASP.NET Web API 中的传入请求,类似于 WCF AfterReceiveRequest(IDispatchMessageInspector 成员),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23953830/

相关文章:

wcf - 在 WCF 中,超时是否会使 channel 出错?

wcf - Restfull WCF Web 服务的 WebDav 问题

asp.net-mvc - 如何覆盖 Bootstrap 样式?

asp.net - 网络 API 2 : "Sample not available"

asp.net-mvc - OWIN 身份验证和自定义响应

c# - 将 WCF 数据表转换为通用/LINQ 对象以供 silverlight 使用的最佳方法

c# - 来自 wcf 服务的 Silverlight 异常(故障)处理

azure - 将 MVC 授权属性与使用 Azure Active Directory + OWIN 的角色结合使用

javascript - 无循环迭代 IEnumerable 模型 - ASP.NET-MVC5

azure - 我们可以在 azure 托管的 Web api 中拥有类对象的内存限制吗?