【问题标题】:Intercept wcf request/response programmatically以编程方式拦截 wcf 请求/响应
【发布时间】:2011-11-30 14:56:05
【问题描述】:

我有一个合同优先 wcf 服务...我想以编程方式捕获服务器的原始肥皂请求/响应并在保存到数据库之前对其进行润色。我需要这个过程来进行一些审计。如何做到这一点?
注意:我不想通过在 web.config 文件中配置来使用 wcf 跟踪来做到这一点。

【问题讨论】:

  • 只是好奇 - 你为什么不想使用跟踪?
  • @TadDonaghe :可能就像我一样,他需要它们给其他人,顺便说一句,是否可以追踪原始消息并保存它们?我期望有时发回的响应不会是 wcf 代理类所期望的,因为众所周知,服务团队会在开发周期后破坏合同并更改 wsdl,需要获取原始请求/响应。

标签: wcf wcf-binding wcf-security wcf-data-services


【解决方案1】:

您可以实现自己的Messageinspector,我不确定这是否足以满足您的需求,但值得一试。

【讨论】:

    【解决方案2】:

    我认为你可以使用SOAP extension

    另一种解决方案可能是编写自定义 HttpModule,您将能够在请求到达 .net 引擎之前对其进行操作

    public class LogModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += this.OnBegin;
        }
    
        private void OnBegin(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = app.Context;
    
            byte[] buffer = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(buffer, 0, buffer.Length);
            context.Request.InputStream.Position = 0;
    
            string soapMessage = Encoding.ASCII.GetString(buffer);
    
            // Do something with soapMessage
        }
    
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2011-01-20
    • 2021-05-05
    • 2018-08-28
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多