.NET Framework Bookmark and Share   
 index > Windows Communication Foundation > MessageHeader Serializer; how to use attributes?
 

MessageHeader Serializer; how to use attributes?

Adding a messageheader (on the client) to a message can be done using several methods:

- in a MessageInspector

- in the requestchannel of a custom channel

- before calling the operation on a service by adding a messageheader to the OperationContact

- in a MessageContract, we can put a MessageHeader atrtibute above a class member (which represents our custom header)

I've tried all four methods, but the first three of them seem to always use the DataContractSerializer to serialize the header, which doesn't support the use of attributes; but my custom header has to have attributes, for example :

<ds:Reference URI="username">

Can't this be done, using the first three methods??

The fourth method works. The servicecontract is decorated with the XmlSerializerFormat which instructs WCF to use the XmlSerializer instead of the default DataContractSerializer. In the first three cases it seems WCF ignores the XmlSerializerFormat attribute on the servicecontract. Why is that?

I really dont want to add the MesageHeader explicitely in myMessageContract! I want it to be added dynamically like using a messageinspector or a custom channel.

Is this possible? Does anybody have experience creating custom (complex!) message headers? And adding these custom headers dynamically?

Thank you!

Robert Jan van Holland

Try writing a custom subclass of MessageHeader and overriding the OnWriteStartHeader method.

Code Block

class MyHeader : MessageHeader

{

protected override void OnWriteStartHeader(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion)

{

base.OnWriteStartHeader(writer, messageVersion);

writer.WriteAttributeString("localname", "ns", "value1234");

}

John Lambert - MSFT

Try writing a custom subclass of MessageHeader and overriding the OnWriteStartHeader method.

Code Block

class MyHeader : MessageHeader

{

protected override void OnWriteStartHeader(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion)

{

base.OnWriteStartHeader(writer, messageVersion);

writer.WriteAttributeString("localname", "ns", "value1234");

}

John Lambert - MSFT
John

I've actually done exactly what you've suggested. This unfortunately breaks xml dig sigs. WCF signs the message based off of the serializer and basically ignores what may or may not have been written to the xml stream.
Alex ORee

You can use google to search for other answers

Custom Search

More Threads

• WCF Service added to a web site project - can not see generated service class
• Inner collections not coming through the pipe
• WCF WS Security Header
• Changing the ServiceDescription at the IServiceBehavior.AddBindingParameters method
• WCF host works in VS2008 but not via debug .exe
• .NET Framework 2.0 client and WCF
• Working with DB stored procedures in WCF
• Feedback and discussion of WCF Essentials Chapter 1
• Move system.serviceModel configuration to a separate config file
• Impersonation using SSL and wsHttpBinding or basicHttpBinding