0

我需要在 IClientMessageInspector 中进行一些操作日志记录,并且知道操作何时开始和结束是至关重要的。但是,我无法为单向操作获取 AfterReceiveReply,这很清楚原因。有没有办法知道在 BeforeSendRequest 重载中操作是单向的,所以我可以忽略它?

4

2 回答 2

1

没有关于检查器本身的信息(或传递给 BeforeSendRequest 的消息),但您可以将此信息传递给检查器,并使用消息操作来查看操作是否是一种方式。

public class StackOverflow_10354828
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
        [OperationContract(IsOneWay = true)]
        void Process(string input);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
        public void Process(string input) { }
    }
    class MyInspector : IClientMessageInspector
    {
        public HashSet<string> oneWayActions;

        public MyInspector(ServiceEndpoint endpoint)
        {
            this.oneWayActions = new HashSet<string>();
            foreach (var operation in endpoint.Contract.Operations)
            {
                if (operation.IsOneWay)
                {
                    oneWayActions.Add(operation.Messages[0].Action);
                }
            }
        }

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            Console.WriteLine("In AfterReceiveReply");
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            Console.WriteLine("In BeginSendRequest");
            if (this.oneWayActions.Contains(request.Headers.Action))
            {
                Console.WriteLine("This is a one-way operation");
            }

            return null;
        }
    }
    class MyBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new MyInspector(endpoint));
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        factory.Endpoint.Behaviors.Add(new MyBehavior());
        ITest proxy = factory.CreateChannel();
        proxy.Echo("Hello");
        Console.WriteLine();

        proxy.Process("world");
        Console.WriteLine();

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
于 2012-04-27T23:16:19.320 回答
1

在我这样做的那一刻回复自己:

bool isOneWay = request.Headers.ReplyTo == null;

于 2012-05-01T09:31:51.097 回答