1

I am in .NET environment and calling a Java SOAP Webservice. I added the Service as a Web Reference and it loaded Proxy and config settings for me. So far so good.

Now, to me it is just like calling another library because all the types are loaded and I can call methods and assign the return types to the Proxy classes that .Net generated for me.

However, my provider says that they are sending some cookies in the SOAP Header. Now, how do I access SOAP header from my C# code?Because when I call the service and retrieve the response, it is like calling a method and retrieving a Typed response. Not sure how to extract SOAP header information from it.

Any ideas?

Lost
  • 12,007
  • 32
  • 121
  • 193

1 Answers1

3

You can use OperationContext class: https://msdn.microsoft.com/pt-br/library/system.servicemodel.operationcontext%28v=vs.110%29.aspx

Use the OperationContext from within a service operation to access the current operation execution environment. In particular, the operation context is used to access callback channels in duplex services, to store extra state data across portions of the operations, and to access incoming message headers and properties as well as add outgoing message headers and properties.

You can read information from Header like this (replace "Cookie" with the tag name that you want to read):

var cookieIndex = OperationContext.Current.IncomingMessageHeaders.FindHeader("Cookie", "");
XmlReader reader = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(cookieIndex).ReadSubtree();

Hope it helps

Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
  • 1
    My OperationContext.Current is Null. Wouldn't this be the case if I am on the Client side. It says that you will have a context if you are on the server and the client calls you ? – Lost Sep 24 '15 at 20:59
  • 2
    On the client, you must [create an `OperationContextScope` around the service call](https://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontextscope(v=vs.110).aspx), best done with the `using` statement. – acelent Sep 24 '15 at 22:54
  • 1
    AS @PauloMadeira noticed, you need a scope of OperationContext. It depends on how you create your client. This post has good information about: http://stackoverflow.com/questions/15205337/current-operationcontext-is-null-in-wcf-windows-service – Ricardo Pontual Sep 25 '15 at 12:28