﻿using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Hardcore.Web.Wcf
{
    class RequestDumpMessageInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            var ctx = new RequestDumpOperationContextExtension(request.ToString());
            OperationContext.Current.Extensions.Add(ctx);

            return request.Headers.MessageId;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            OperationContext.Current.Extensions.Remove(RequestDumpOperationContextExtension.Current);
        }
    }
}
