2012-04-27 10 views
0

IClientMessageInspectorにいくつかの操作ログを記録する必要があり、操作の開始と終了が重要であることがわかっています。しかし、私はAfterReceiveReplyを一方向の操作で得ることはできませんが、それはなぜか分かりません。 BeforeSendRequestのオーバーロードで操作が片方向であることを知る方法はありますか?私はそれを無視できますか?IClientMessageInspectorは片方向操作を検出します

答えて

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(); 
    } 
} 
1

は、現時点では私はこれやってる:

ブールisOneWay = request.Headers.ReplyTo == nullを。

関連する問題