none
WCF REST: Find correct format at IClientMessageInspectorAfterReceiveReply

    Question

  • Hi

    I need to find out message type (XML or JSON) already in IClientMessageInspectorAfterReceiveReply.

    Don't seem to find an easy solution to this one. I recon I should open OperationContextScope in some way?

     

    Kind regards

    A.H.

    Wednesday, February 23, 2011 12:47 PM

Answers

  • You can check the WebBodyFormatMessageProperty in the reply properties. If you use the WebMessageEncodingBindingElement (which is used by WebHttpBinding), it will be populated with the actual format of the incoming message.

      public class Post_55d9c995_c857_4ae4_b1d2_55cd1039bc35
      {
        [ServiceContract]
        public interface ITest
        {
          [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
          string EchoJson(string text);
          [WebInvoke(ResponseFormat = WebMessageFormat.Xml)]
          string EchoXml(string text);
        }
        public class Service : ITest
        {
          public string EchoJson(string text) { return text; }
          public string EchoXml(string text) { return text; }
        }
        class MyInspector : IEndpointBehavior, IClientMessageInspector
        {
          public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
          {
          }
    
          public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
          {
            clientRuntime.MessageInspectors.Add(this);
          }
    
          public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
          {
          }
    
          public void Validate(ServiceEndpoint endpoint)
          {
          }
    
          public void AfterReceiveReply(ref Message reply, object correlationState)
          {
            if (reply.Properties.ContainsKey(WebBodyFormatMessageProperty.Name))
            {
              WebBodyFormatMessageProperty prop = (WebBodyFormatMessageProperty)reply.Properties[WebBodyFormatMessageProperty.Name];
              Console.WriteLine("Reply format: {0}", prop.Format);
            }
          }
    
          public object BeforeSendRequest(ref Message request, IClientChannel channel)
          {
            return null;
          }
        }
        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 WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
          host.Open();
          Console.WriteLine("Host opened");
    
          ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WebHttpBinding(), new EndpointAddress(baseAddress));
          factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
          factory.Endpoint.Behaviors.Add(new MyInspector());
          ITest proxy = factory.CreateChannel();
    
          Console.WriteLine("Calling EchoJson");
          proxy.EchoJson("EchoJson");
    
          Console.WriteLine();
          Console.WriteLine("Calling EchoXml");
          proxy.EchoXml("EchoXml");
    
          ((IClientChannel)proxy).Close();
          factory.Close();
    
          Console.Write("Press ENTER to close the host");
          Console.ReadLine();
          host.Close();
        }
      }
    
    
    • Marked as answer by Homer22 Wednesday, February 23, 2011 7:03 PM
    Wednesday, February 23, 2011 4:26 PM

All replies

  • You can check the WebBodyFormatMessageProperty in the reply properties. If you use the WebMessageEncodingBindingElement (which is used by WebHttpBinding), it will be populated with the actual format of the incoming message.

      public class Post_55d9c995_c857_4ae4_b1d2_55cd1039bc35
      {
        [ServiceContract]
        public interface ITest
        {
          [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
          string EchoJson(string text);
          [WebInvoke(ResponseFormat = WebMessageFormat.Xml)]
          string EchoXml(string text);
        }
        public class Service : ITest
        {
          public string EchoJson(string text) { return text; }
          public string EchoXml(string text) { return text; }
        }
        class MyInspector : IEndpointBehavior, IClientMessageInspector
        {
          public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
          {
          }
    
          public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
          {
            clientRuntime.MessageInspectors.Add(this);
          }
    
          public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
          {
          }
    
          public void Validate(ServiceEndpoint endpoint)
          {
          }
    
          public void AfterReceiveReply(ref Message reply, object correlationState)
          {
            if (reply.Properties.ContainsKey(WebBodyFormatMessageProperty.Name))
            {
              WebBodyFormatMessageProperty prop = (WebBodyFormatMessageProperty)reply.Properties[WebBodyFormatMessageProperty.Name];
              Console.WriteLine("Reply format: {0}", prop.Format);
            }
          }
    
          public object BeforeSendRequest(ref Message request, IClientChannel channel)
          {
            return null;
          }
        }
        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 WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
          host.Open();
          Console.WriteLine("Host opened");
    
          ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WebHttpBinding(), new EndpointAddress(baseAddress));
          factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
          factory.Endpoint.Behaviors.Add(new MyInspector());
          ITest proxy = factory.CreateChannel();
    
          Console.WriteLine("Calling EchoJson");
          proxy.EchoJson("EchoJson");
    
          Console.WriteLine();
          Console.WriteLine("Calling EchoXml");
          proxy.EchoXml("EchoXml");
    
          ((IClientChannel)proxy).Close();
          factory.Close();
    
          Console.Write("Press ENTER to close the host");
          Console.ReadLine();
          host.Close();
        }
      }
    
    
    • Marked as answer by Homer22 Wednesday, February 23, 2011 7:03 PM
    Wednesday, February 23, 2011 4:26 PM
  • Thx Carlos, you are a lifesaver.

    Wednesday, February 23, 2011 7:02 PM