使用 New-WebServiceProxy 在 PowerShell 中添加自定义 SOAP 标头
在 C# 中,我可以执行以下操作:
var header = MessageHeader.CreateHeader("MyHeader", "http://mynamespace", "Header value");
OperationContext.Current.OutgoingMessageHeaders.Add(header);
将以下内容添加到 SOAP 消息中:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<MyHeader xmlns="http://mynamespace">Header value</MyHeader>
....
</s:Header>
...
在调用 New-WebServiceProxy PowerShell commandlet 生成的代理上的方法时,如何类似地添加自定义传出 SOAP 消息标头?
编辑: 为了澄清这一点,我可以在 PowerShell 中进行与上面 C# 中显示的相同调用,但 OperationContext.Current 始终为 null。我在 C# 中通过创建一个 OperationContextScope 来解决这个问题,但这需要 Web 服务代理的内部通道,而 PowerShell 的代理似乎没有提供这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 C# 代码正在使用 WCF 中的 OperationContext 类。 PowerShell 的 New-WebServiceProxy cmdlet 不使用 WCF,而是基于 System.Web.Services.Protocols.SoapHttpClientProtocol 类创建类。
要获取 SoapHttpClientProtocol 的实例来发送自定义 SOAP 标头,请使用 SoapExtension,如下所述:
http://blogs.msdn.com/b/kaevans/archive/2007/08/06/programmatically-insert-soapheader-into-soap-request-with-asmx-soapextensions.aspx
由于需要创建一个继承自 SoapExtension 的新类,将博客文章的内容移植到 PowerShell 很可能涉及通过 Add-Type cmdlet 的 TypeDefinition 参数使用嵌入式 C#。
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.