4

In C# I can do the following:

var header = MessageHeader.CreateHeader("MyHeader", "http://mynamespace", "Header value");
OperationContext.Current.OutgoingMessageHeaders.Add(header);

That adds the following to the SOAP message:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <MyHeader xmlns="http://mynamespace">Header value</MyHeader>
        ....
    </s:Header>
...

How can I similarly add a custom outgoing SOAP message header when calling methods on a proxy generated by the New-WebServiceProxy PowerShell commandlet?

Edit: To clarify, I can make the same calls in PowerShell that I show in the C# above, but OperationContext.Current is always null. I get around that in C# by creating an OperationContextScope, but that requires the inner channel of the web service proxy, which PowerShell's proxy doesn't seem to provide.

dcstraw
  • 3,243
  • 3
  • 29
  • 38
  • To clarify, I can make the same calls in PowerShell that I show in the C# above, but OperationContext.Current is always null. I get around that in C# by creating an OperationContextScope, but that requires the inner channel of the web service proxy, which PowerShell's proxy doesn't seem to provide. – dcstraw Jan 21 '11 at 22:30
  • please update your question with this clarification. – John Saunders Jan 21 '11 at 23:41
  • FYI I ended up going with setting cookies using the proxy's CookieContainer to pass the information I needed. From Jason's answer it looks like my original goal may be possible but somewhat involved. If I had it to do over again I would probably go with a WCF proxy generated with svcutil. – dcstraw Feb 15 '11 at 18:19

1 Answers1

4

Your C# code is using the OperationContext class from WCF. PowerShell's New-WebServiceProxy cmdlet does not use WCF, instead it creates classes based on the System.Web.Services.Protocols.SoapHttpClientProtocol class.

To get an instance of SoapHttpClientProtocol to send custom SOAP headers, you use SoapExtension, as described here: http://blogs.msdn.com/b/kaevans/archive/2007/08/06/programmatically-insert-soapheader-into-soap-request-with-asmx-soapextensions.aspx

Due to the need to create a new class inheriting from SoapExtension, porting the content of the blog post to PowerShell will most likely involve use of embedded C# via the Add-Type cmdlet's TypeDefinition parameter.

Jason Stangroome
  • 4,459
  • 3
  • 33
  • 39
  • Thanks for the clarification and suggestion. I'm starting to think that the best way to go may be to call out to svcutil to create a WCF service proxy and then load the resulting assembly. However your answer seems like a good one for the question I asked. – dcstraw Feb 15 '11 at 18:14
  • one usually uses the `[SoapHeader]` attribute to send custom SOAP headers. I don't know if that can be used in this scenario, however. – John Saunders Feb 15 '11 at 20:46