Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WCF Custom MessageHeader

WCF Custom MessageHeader

Scheduled Pinned Locked Moved WPF
csharpwcfsysadmin
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K koleraba

    Hi I am triing to send some aditional info from wcf client to server using custom MessageHeader. Below is the class which should be sent inside message header.

    public class UserCredentialsHeaderContent
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    
        public UserCredentialsHeaderContent(string userName, string password)
        {
            UserName = userName;
            Password = password;
        }
    
        public UserCredentialsHeaderContent()
        {
    
        }
    }
    

    And the custom MessageHeader:

    public class UserCredentialsMessageHeader : MessageHeader
    {
    private const string HEADER_NAME = "UserToken";
    private const string HEADER_NAMESPACE = "NameSpace";

        public UserCredentialsHeaderContent UserCredentials { get; private set; }
    
        public override string Name
        {
            get { return HEADER\_NAME; }
        }
    
        public override string Namespace
        {
            get { return HEADER\_NAMESPACE; }
        }
    
        public UserCredentialsMessageHeader()
        {
    
        }
    
        public UserCredentialsMessageHeader(UserCredentialsHeaderContent userCredentials)
        {
            UserCredentials = userCredentials;
        }
    
        protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
            StringWriter stringWriter = new StringWriter();
            serializer.Serialize(stringWriter, UserCredentials);
            stringWriter.Close();
    
            string text = stringWriter.ToString();
            writer.WriteElementString(HEADER\_NAME, HEADER\_NAMESPACE, text);
        }
    
        public static UserCredentialsHeaderContent ReadHeader(Message request)
        {
            int headerIndex = request.Headers.FindHeader(HEADER\_NAME, HEADER\_NAMESPACE);
            if (headerIndex != -1)
            {
                XmlNode\[\] contentNode = request.Headers.GetHeader(headerIndex);
                string text = contentNode\[0\].InnerText;
    
                XmlSerializer deSerializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
                StringReader stringReader = new StringReader(text);
                UserCredentialsHeaderContent content = (UserCredentialsHeaderContent)  deSerializer.Deserialize(stringReader);
    
    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    I think you need to post this in the C# .NET this is the WPF / Silverlight venue. :cool:

    "Make everything as simple as possible, but not simpler." -- Albert Einstein

    M D 2 Replies Last reply
    0
    • K koleraba

      Hi I am triing to send some aditional info from wcf client to server using custom MessageHeader. Below is the class which should be sent inside message header.

      public class UserCredentialsHeaderContent
      {
          public string UserName { get; set; }
          public string Password { get; set; }
      
          public UserCredentialsHeaderContent(string userName, string password)
          {
              UserName = userName;
              Password = password;
          }
      
          public UserCredentialsHeaderContent()
          {
      
          }
      }
      

      And the custom MessageHeader:

      public class UserCredentialsMessageHeader : MessageHeader
      {
      private const string HEADER_NAME = "UserToken";
      private const string HEADER_NAMESPACE = "NameSpace";

          public UserCredentialsHeaderContent UserCredentials { get; private set; }
      
          public override string Name
          {
              get { return HEADER\_NAME; }
          }
      
          public override string Namespace
          {
              get { return HEADER\_NAMESPACE; }
          }
      
          public UserCredentialsMessageHeader()
          {
      
          }
      
          public UserCredentialsMessageHeader(UserCredentialsHeaderContent userCredentials)
          {
              UserCredentials = userCredentials;
          }
      
          protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
          {
              XmlSerializer serializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
              StringWriter stringWriter = new StringWriter();
              serializer.Serialize(stringWriter, UserCredentials);
              stringWriter.Close();
      
              string text = stringWriter.ToString();
              writer.WriteElementString(HEADER\_NAME, HEADER\_NAMESPACE, text);
          }
      
          public static UserCredentialsHeaderContent ReadHeader(Message request)
          {
              int headerIndex = request.Headers.FindHeader(HEADER\_NAME, HEADER\_NAMESPACE);
              if (headerIndex != -1)
              {
                  XmlNode\[\] contentNode = request.Headers.GetHeader(headerIndex);
                  string text = contentNode\[0\].InnerText;
      
                  XmlSerializer deSerializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
                  StringReader stringReader = new StringReader(text);
                  UserCredentialsHeaderContent content = (UserCredentialsHeaderContent)  deSerializer.Deserialize(stringReader);
      
      H Offline
      H Offline
      hb52134214
      wrote on last edited by
      #3

      Do message headers had to be attached before any content is appended? Is it possible that you've added the message too late in the cycle? Can you attach it earlier?

      K 1 Reply Last reply
      0
      • L Lost User

        I think you need to post this in the C# .NET this is the WPF / Silverlight venue. :cool:

        "Make everything as simple as possible, but not simpler." -- Albert Einstein

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #4

        TheArchitectmc∞ wrote:

        this is the WPF / Silverlight venue

        hmm...ok. :rolleyes:

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        L 1 Reply Last reply
        0
        • L Lost User

          I think you need to post this in the C# .NET this is the WPF / Silverlight venue. :cool:

          "Make everything as simple as possible, but not simpler." -- Albert Einstein

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #5

          The title of this forum is WPF / WCF / WF

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          L 1 Reply Last reply
          0
          • D DaveyM69

            The title of this forum is WPF / WCF / WF

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            Darn I guess I got whacked with the idiot stick today! :omg:

            "Make everything as simple as possible, but not simpler." -- Albert Einstein "It didn't matter to us whether people believed in us. We believed in ourselves. We had the courage to follow our own path." ~~Nvidia's Jen-Hsun Huang

            D 1 Reply Last reply
            0
            • M Mark Salsbery

              TheArchitectmc∞ wrote:

              this is the WPF / Silverlight venue

              hmm...ok. :rolleyes:

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              My apologies, this is the right place big guy....

              "Make everything as simple as possible, but not simpler." -- Albert Einstein "It didn't matter to us whether people believed in us. We believed in ourselves. We had the courage to follow our own path." ~~Nvidia's Jen-Hsun Huang

              1 Reply Last reply
              0
              • K koleraba

                Hi I am triing to send some aditional info from wcf client to server using custom MessageHeader. Below is the class which should be sent inside message header.

                public class UserCredentialsHeaderContent
                {
                    public string UserName { get; set; }
                    public string Password { get; set; }
                
                    public UserCredentialsHeaderContent(string userName, string password)
                    {
                        UserName = userName;
                        Password = password;
                    }
                
                    public UserCredentialsHeaderContent()
                    {
                
                    }
                }
                

                And the custom MessageHeader:

                public class UserCredentialsMessageHeader : MessageHeader
                {
                private const string HEADER_NAME = "UserToken";
                private const string HEADER_NAMESPACE = "NameSpace";

                    public UserCredentialsHeaderContent UserCredentials { get; private set; }
                
                    public override string Name
                    {
                        get { return HEADER\_NAME; }
                    }
                
                    public override string Namespace
                    {
                        get { return HEADER\_NAMESPACE; }
                    }
                
                    public UserCredentialsMessageHeader()
                    {
                
                    }
                
                    public UserCredentialsMessageHeader(UserCredentialsHeaderContent userCredentials)
                    {
                        UserCredentials = userCredentials;
                    }
                
                    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
                        StringWriter stringWriter = new StringWriter();
                        serializer.Serialize(stringWriter, UserCredentials);
                        stringWriter.Close();
                
                        string text = stringWriter.ToString();
                        writer.WriteElementString(HEADER\_NAME, HEADER\_NAMESPACE, text);
                    }
                
                    public static UserCredentialsHeaderContent ReadHeader(Message request)
                    {
                        int headerIndex = request.Headers.FindHeader(HEADER\_NAME, HEADER\_NAMESPACE);
                        if (headerIndex != -1)
                        {
                            XmlNode\[\] contentNode = request.Headers.GetHeader(headerIndex);
                            string text = contentNode\[0\].InnerText;
                
                            XmlSerializer deSerializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
                            StringReader stringReader = new StringReader(text);
                            UserCredentialsHeaderContent content = (UserCredentialsHeaderContent)  deSerializer.Deserialize(stringReader);
                
                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #8

                koleraba wrote:

                But all Properties of the UserCredentialsHeaderContent are null.

                I don't see any place you're calling the deserialization code. Am I missing something?

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                K 1 Reply Last reply
                0
                • L Lost User

                  Darn I guess I got whacked with the idiot stick today! :omg:

                  "Make everything as simple as possible, but not simpler." -- Albert Einstein "It didn't matter to us whether people believed in us. We believed in ourselves. We had the courage to follow our own path." ~~Nvidia's Jen-Hsun Huang

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #9

                  I know the feeling :-D :thumbsup:

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    koleraba wrote:

                    But all Properties of the UserCredentialsHeaderContent are null.

                    I don't see any place you're calling the deserialization code. Am I missing something?

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    K Offline
                    K Offline
                    koleraba
                    wrote on last edited by
                    #10

                    No you are not missing it. When I was implementing the custrom header class, I was looking which metod to overrite to deserialize the header, but I did not find anything. I know that serialization should be done in the OnWriteHeaderContent but there is no OnReadHeaderContent. Do you know where the deserialization code should be. Thank you for your reply. Uros

                    M 1 Reply Last reply
                    0
                    • H hb52134214

                      Do message headers had to be attached before any content is appended? Is it possible that you've added the message too late in the cycle? Can you attach it earlier?

                      K Offline
                      K Offline
                      koleraba
                      wrote on last edited by
                      #11

                      I think that the header is addad at the correct time, but I guess there is a problem with deserialization. Thank you for your reply Uros

                      1 Reply Last reply
                      0
                      • K koleraba

                        No you are not missing it. When I was implementing the custrom header class, I was looking which metod to overrite to deserialize the header, but I did not find anything. I know that serialization should be done in the OnWriteHeaderContent but there is no OnReadHeaderContent. Do you know where the deserialization code should be. Thank you for your reply. Uros

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #12

                        koleraba wrote:

                        Do you know where the deserialization code should be.

                        A ReadHeader method is in your class, but you need to call it. I modified the parameter type of your code like this:

                        public static UserCredentialsHeaderContent ReadHeader(MessageHeaders headers)
                        {
                            int headerIndex = headers.FindHeader(HEADER\_NAME, HEADER\_NAMESPACE);
                            if (headerIndex != -1)
                            {
                                XmlNode\[\] contentNode = headers.GetHeader<XmlNode\[\]>(headerIndex);
                                string text = contentNode\[0\].InnerText;
                        
                                XmlSerializer deSerializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
                                StringReader stringReader = new StringReader(text);
                                UserCredentialsHeaderContent content = (UserCredentialsHeaderContent)deSerializer.Deserialize(stringReader);
                                stringReader.Close();
                        
                                return content;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        

                        and called it like this

                        UserCredentialsHeaderContent headercontent = UserCredentialsMessageHeader.ReadHeader(OperationContext.Current.IncomingMessageHeaders);

                        Works fine.

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        K 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          koleraba wrote:

                          Do you know where the deserialization code should be.

                          A ReadHeader method is in your class, but you need to call it. I modified the parameter type of your code like this:

                          public static UserCredentialsHeaderContent ReadHeader(MessageHeaders headers)
                          {
                              int headerIndex = headers.FindHeader(HEADER\_NAME, HEADER\_NAMESPACE);
                              if (headerIndex != -1)
                              {
                                  XmlNode\[\] contentNode = headers.GetHeader<XmlNode\[\]>(headerIndex);
                                  string text = contentNode\[0\].InnerText;
                          
                                  XmlSerializer deSerializer = new XmlSerializer(typeof(UserCredentialsHeaderContent));
                                  StringReader stringReader = new StringReader(text);
                                  UserCredentialsHeaderContent content = (UserCredentialsHeaderContent)deSerializer.Deserialize(stringReader);
                                  stringReader.Close();
                          
                                  return content;
                              }
                              else
                              {
                                  return null;
                              }
                          }
                          

                          and called it like this

                          UserCredentialsHeaderContent headercontent = UserCredentialsMessageHeader.ReadHeader(OperationContext.Current.IncomingMessageHeaders);

                          Works fine.

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          K Offline
                          K Offline
                          koleraba
                          wrote on last edited by
                          #13

                          Mark thank you for your time. In the mean time I found the same solution and it works fine. But as you said you have to call the deserialization code by your self - there isn't any method that you could override. Thanks again for your time Uroš

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups