WCF Custom MessageHeader
-
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
TheArchitectmc∞ wrote:
this is the WPF / Silverlight venue
hmm...ok. :rolleyes:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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
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) -
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)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
-
TheArchitectmc∞ wrote:
this is the WPF / Silverlight venue
hmm...ok. :rolleyes:
Mark Salsbery Microsoft MVP - Visual C++ :java:
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
-
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);
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:
-
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
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) -
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:
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
-
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?
-
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
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:
-
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: