none
Need example on implementing MessageHeaders.WriteHeaderContents RRS feed

  • Question

  • I have a custom header stored in a "string" variable, I need to replace the header of a outgoing SOAP request from my WCF client with the header in the "string" type variable. Based on research I see that implementing the MessageHeaders.WriteHeaderContents can work but this method accepts only XMLDictionaryWriter or XMLWriter types as input. I have a string input. How do i code in C# ?  if WriteHeaderContents is not the way to go ,please provide alternate options with good example illustrating string as input.

    PLEASE HELP!!!!!!!!!


    VRS
    Monday, August 22, 2011 12:47 AM

Answers

  • Message headers are a SOAP concept, and SOAP requests are XML documents, so you really need some XML-ness there. But for your scenario, you actually don't need to override MessageHeaders, you can use an inspector and simply replace the header at that point, as shown in the example below (the conversion to XML will be done by the MessageHeader class).

      public class Post_2ee8f228_b01b_4e80_9335_8c7fa23c9dd0
      {
        [MessageContract]
        public class MyMC
        {
          [MessageHeader(Name = "MyHeader", Namespace = "http://my.namespace.com")]
          public string HeaderValue { get; set; }
          [MessageBodyMember(Name = "MyBody", Namespace = "http://my.namespace.com")]
          public string BodyValue { get; set; }
        }
        [ServiceContract]
        public interface ITest
        {
          [OperationContract]
          void Process(MyMC mc);
        }
        public class Service : ITest
        {
          public void Process(MyMC mc)
          {
            Console.WriteLine("Header value: {0}", mc.HeaderValue);
          }
        }
        public class MyInspector : IEndpointBehavior, IClientMessageInspector
        {
          public string NewHeaderValue { get; set; }
    
          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)
          {
          }
    
          public object BeforeSendRequest(ref Message request, IClientChannel channel)
          {
            int originalIndex = request.Headers.FindHeader("MyHeader", "http://my.namespace.com");
            if (originalIndex >= 0)
            {
              request.Headers.Insert(originalIndex, MessageHeader.CreateHeader("MyHeader", "http://my.namespace.com", this.NewHeaderValue));
              request.Headers.RemoveAt(originalIndex + 1);
            }
    
            return null;
          }
        }
        public static void Test()
        {
          string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
          ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
          ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new WSHttpBinding(), "");
          host.Open();
          Console.WriteLine("Host opened");
    
          ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WSHttpBinding(), new EndpointAddress(baseAddress));
          MyInspector inspector = new MyInspector { NewHeaderValue = "Modified header value" };
          factory.Endpoint.Behaviors.Add(inspector);
          ITest proxy = factory.CreateChannel();
    
          proxy.Process(new MyMC { HeaderValue = "Original header value", BodyValue = "The body" });
    
          ((IClientChannel)proxy).Close();
          factory.Close();
    
          Console.Write("Press ENTER to close the host");
          Console.ReadLine();
          host.Close();
        }
      }
    
    

     


    Carlos Figueira
    • Marked as answer by Waazu Tuesday, August 23, 2011 6:45 PM
    Monday, August 22, 2011 2:34 PM
    Moderator

All replies

  • Have you tried something like the following

     

    var myHeader = new MyHeader { MyFirstValue = "Yeeehaaaw!!", MyOtherValue = "Gaaah!" };

    var messageHeader = new MessageHeader<MyHeader>() { Actor = "Anyone", Content = myHeader };

    request.Headers.Add(messageHeader.GetUntypedHeader("MyHeader""http://tempuri.org/"));

     

    Cheers,

    Bali

     


    --------------------------------------
    Mark As Answer or Vote As Helpful if this helps.
    Monday, August 22, 2011 6:16 AM
  • Message headers are a SOAP concept, and SOAP requests are XML documents, so you really need some XML-ness there. But for your scenario, you actually don't need to override MessageHeaders, you can use an inspector and simply replace the header at that point, as shown in the example below (the conversion to XML will be done by the MessageHeader class).

      public class Post_2ee8f228_b01b_4e80_9335_8c7fa23c9dd0
      {
        [MessageContract]
        public class MyMC
        {
          [MessageHeader(Name = "MyHeader", Namespace = "http://my.namespace.com")]
          public string HeaderValue { get; set; }
          [MessageBodyMember(Name = "MyBody", Namespace = "http://my.namespace.com")]
          public string BodyValue { get; set; }
        }
        [ServiceContract]
        public interface ITest
        {
          [OperationContract]
          void Process(MyMC mc);
        }
        public class Service : ITest
        {
          public void Process(MyMC mc)
          {
            Console.WriteLine("Header value: {0}", mc.HeaderValue);
          }
        }
        public class MyInspector : IEndpointBehavior, IClientMessageInspector
        {
          public string NewHeaderValue { get; set; }
    
          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)
          {
          }
    
          public object BeforeSendRequest(ref Message request, IClientChannel channel)
          {
            int originalIndex = request.Headers.FindHeader("MyHeader", "http://my.namespace.com");
            if (originalIndex >= 0)
            {
              request.Headers.Insert(originalIndex, MessageHeader.CreateHeader("MyHeader", "http://my.namespace.com", this.NewHeaderValue));
              request.Headers.RemoveAt(originalIndex + 1);
            }
    
            return null;
          }
        }
        public static void Test()
        {
          string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
          ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
          ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new WSHttpBinding(), "");
          host.Open();
          Console.WriteLine("Host opened");
    
          ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WSHttpBinding(), new EndpointAddress(baseAddress));
          MyInspector inspector = new MyInspector { NewHeaderValue = "Modified header value" };
          factory.Endpoint.Behaviors.Add(inspector);
          ITest proxy = factory.CreateChannel();
    
          proxy.Process(new MyMC { HeaderValue = "Original header value", BodyValue = "The body" });
    
          ((IClientChannel)proxy).Close();
          factory.Close();
    
          Console.Write("Press ENTER to close the host");
          Console.ReadLine();
          host.Close();
        }
      }
    
    

     


    Carlos Figueira
    • Marked as answer by Waazu Tuesday, August 23, 2011 6:45 PM
    Monday, August 22, 2011 2:34 PM
    Moderator