C#确定WCF输入和输出消息的大小

时间:2019-02-04 11:37:07

标签: c# wcf

我试图弄清楚如何在WMF消息大小在客户端端从DTO对象反序列化/串行化之前确定该大小:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    MyResponse SampleMethod(MyRequest request);
}

当前,我有这个消息检查器来记录请求及其在客户端的执行时间:

internal sealed class LoggingMessageInspector : IClientMessageInspector
{
    public event EventHandler<State> OnBegin;
    public event EventHandler<State> OnEnd;

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var state = new State
                    {
                        MethodName = GetMethodName(request)
                    };
        OnBegin?.Invoke(this, state);
        state.Timer = Stopwatch.StartNew();
        return state;
    }

    private string GetMethodName(Message msg)
    {
        try
        {
            return Path.GetFileName(msg.Headers.Action);
        }
        catch
        {
            return null;
        }
    }
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        var state = correlationState as State;
        if(state == null)
            return;

        state.Timer.Stop();
        OnEnd?.Invoke(this, state);
    }
}

正如我在Message类的ToString方法的实现中所看到的-它正在执行XML序列化,仅对字节计数是过大的。我还能做些什么来使客户每次呼叫的消息大小都得到?另外,如何计算通过有线实际传输的消息大小?

0 个答案:

没有答案