4

在 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 消息头?

编辑:为了澄清,我可以在上面的 C# 中显示的 PowerShell 中进行相同的调用,但 OperationContext.Current 始终为空。我通过创建 OperationContextScope 在 C# 中解决了这个问题,但这需要 Web 服务代理的内部通道,而 PowerShell 的代理似乎没有提供。

4

1 回答 1

4

您的 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-肥皂请求与 asmx-soapextensions.aspx

由于需要创建一个从 SoapExtension 继承的新类,将博客文章的内容移植到 PowerShell 很可能涉及通过 Add-Type cmdlet 的 TypeDefinition 参数使用嵌入式 C#。

于 2011-02-15T02:52:28.613 回答