In C# I can do the following:
var h开发者_如何学Goeader = 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.
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.
精彩评论