Add a custom header to an client message

Posted by Lomeco under WCF on 3/7/2011 | Points: 10 | Views : 4155 | Status : [Member] | Replies : 4
Hi,

I am trying to add a custom header to a message of a Webservice, which I can not change. When I add the Serfice Referenc to my app, it did not create an entry inside of my app.config. So I createt a proxyfile with wsdl.exe. Which has all the parts I need.

But the message I generate by default is not accepted by the server, because some parts in the header are missing. So I createt a SoapHeader class:


Public Class Security
Inherits SoapHeader

Public Token As Xml.XmlElement
Public Sub New()
Dim Doc As New XmlDocument()
Dim myXMLSubElement As Xml.XmlElement
myXMLSubElement = Doc.CreateElement( "Identifier")
myXMLSubElement.InnerText = Janitos. MessageInspector.allNodes(0).ToString
Token = Doc.CreateElement( "soapenv", "Token", "http://schemas.xmlsoap.org/ws/2005/02/sc")
Token.AppendChild(myXMLSubElement)
End Sub
End Class


But when I instanciate my new Header, and try to create the message like this:


Public MyHeader As New Security

<SoapHeader("MyHeader", Direction:=SoapHeaderDirection.In), _
System.Web.Services.Protocols.SoapDocumentMethodAttribute("urn:getEVBAbruf", RequestNamespace:="http://www.xyz.net/namespace/evb", ResponseNamespace:="http://www.xyz.net/namespace/evb", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function getAnswer(ByVal Request As CT_AnswerRequest) As <System.Xml.Serialization.XmlElementAttribute("Response")> CT_EVBAbrufResponse

Dim Doc As New XmlDocument()
MyHeader.SecurityContextToken.Prefix = "wsc"
MyHeader.MustUnderstand = "1"
Dim results() As Object = Me.Invoke("getAnswer", New Object() {Request})
Return CType(results(0), CT_EVBAbrufResponse)
End Function


I'll get this message:


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Header>
- <Security soap:mustUnderstand="1" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
- <Token>
- <wsc:Token xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc">
<Identifier xmlns="">wsBu7GHhFcb+GOfT7cvJn5Gdmsmdn7DP+GgzfunG/4cVSQBDnJTJfYkm3CpVgn476D</Identifier>
</wsc:Token>
</Token>
</Security>
</soap:Header>
- <soap:Body>


It creates two entries "Token"( - <Token> AND- <wsc:Token) inside my header, but I need it only once. What is wrong in my header creation?

regards Lothar




Responses

Posted by: Gsolvers on: 3/7/2011 [Member] Starter | Points: 25

Up
0
Down
Token = Doc.CreateElement( "soapenv", "Token", "http://schemas.xmlsoap.org/ws/2005/02/sc")

Token.AppendChild(myXMLSubElement)


This is creating the First Level Token , you can get rid of this code.

Best Regards,

VG
www.TeacherJi.com

Lomeco, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 3/7/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Did u give service reference ?? .. try using web reference instead..for web service.

Thanks and Regards
Madhu

Lomeco, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lomeco on: 3/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

if I leave the two lines out

Token = Doc.CreateElement( "soapenv", "Token", "http://schemas.xmlsoap.org/ws/2005/02/sc")

Token.AppendChild(myXMLSubElement)

I'll get

Object reference not set to an instance of an object.

when I try to set the prefix for token
MyHeader.Token.Prefix = "wsc"


Lomeco, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lomeco on: 3/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Madhu,

it makes no difference to the fact that I have ti customize the header of this message.

Lothar

Lomeco, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response