如何登录WCF服务的原始请求 [英] How to log the raw request in WCF service

查看:214
本文介绍了如何登录WCF服务的原始请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几种方法WCF服务。我想记录从客户端进来,无论这是如何发送原始请求。一种方法接受数据作为查询字符串(严格用于支持旧版),我可以使用日志:

I have a WCF service with several methods. I would like to log the raw request that came in from the client regardless of how this was sent. One method accepts the data as a querystring (strictly for legacy support) which I can log using:

OperationContext.Current.IncomingMessageHeaders.To.AbsoluteUri

这是在这种情况足够了,但是其他的方法让客户使用由svcutil.exe的生成的代理类来发送数据作为XML。在这种情况下,我发现数据我想在S:身体:

That is sufficient in that scenario, but other methods allow the client to send data as XML using a proxy class generated by svcutil.exe. In this scenario I have found the data I want in the s:Body of:

OperationContext.Current.RequestContext.RequestMessage

不幸的是,不管是什么我尝试读取之前,我无法创建消息的缓冲副本。下面是一个例子:

Unfortunately, no matter what I try I can't create a buffered copy of the message before it is read. Here is an example:

public CascadeResponse SendCustomer(Customer c)
    {
        Message msg = OperationContext.Current.RequestContext.RequestMessage.CreateBufferedCopy(Int32.MaxValue).CreateMessage();
        LogMessage(msg);
        // Now that the request is logged, get on with the rest
    }

在SendCustomer的第一线,不过,我得到以下错误:

On the first line of SendCustomer, however, I get the following error:

因为它已经被读此消息不支持操作。

这是我的创建缓冲副本,想必点?我猜我做得elementally错在这里。

This is the point of me creating the buffered copy, surely? I'm guessing that I'm doing something elementally wrong here.

编辑:

好了,所以该方法是现在这个样子:

Ok, so the method is now like this:

public CascadeResponse SendCustomer(Message requestMessage)
    {
        Message msg = OperationContext.Current.RequestContext.RequestMessage.CreateBufferedCopy(Int32.MaxValue).CreateMessage();
        LogMessage(msg);
        // Now that the request is logged, get on with the rest        
        Customer c = msg.GetBody<Customer>();
        string clientKey = "1111"; // This should be the clientKey string passed to the function along with the customer 
        return SendLead(c, clientKey);
    }

我的问题是,我不知道如何让客户和ClientKey派作为独立的实体。我可以做cl​​ientKey客户的属性(或创建一个自定义的对象,它是专门用于传递数据,并包含客户和ClientKey作为属性),但我想避免,如果可能的话,因为这是一个遗留系统的升级,它已经以这种方式工作。

My problem is that I don't know how to get the Customer and ClientKey sent as separate entities. I could make clientKey a property of Customer (or create a custom object that is specifically for passing data in and contains Customer and ClientKey as attributes), but I would like to avoid that if possible, as this is an upgrade of a legacy system that already works this way.

我也遇到了麻烦svcutil.exe的使用创造我的代理类 - 我认为具有上述方法签名意味着我的服务将不再公布正确的签名发送请求的?不知道这是非常明显的 - 如果我唯一的输入法接受一个Message对象,如何我的客户知道发送一个客户和ClientKey

I am also having trouble using svcUtil.exe to create my proxy classes - I assume that having the above method signature means my service will no longer advertise the correct signature to send requests as? Not sure if that is clear enough - if my only input method accepts a Message object, how does my client know to send a Customer and a ClientKey?

推荐答案

我发现了一个解决方案,人也可能会发现有用。创建MessageInspector让您code连接到AfterReceiveRequest和BeforeSendReply事件,按以下内容:

I found a solution that others might also find useful. Creating a MessageInspector allows you to attach code to the "AfterReceiveRequest" and "BeforeSendReply" events, as per the following:

public class MessageInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        LogMessage("Received:\n{0}", buffer.CreateMessage().ToString());
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
        reply = buffer.CreateMessage();
        LogMessage("Sending:\n{0}", buffer.CreateMessage().ToString());
    }
}

有对于建立消息的全教程督察FO WCF这里。我会说注意检查时要添加的行为扩展到你的app.config / web.config中完全限定的程序集名称。

There is a full tutorial for setting up message inspectors fo wcf here. I will say be careful to check your fully qualified assembly name when you are adding the behavior extension to your app.config/web.config.

希望别人认为这很有用。

Hope someone else finds this useful.

这篇关于如何登录WCF服务的原始请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆