1

我使用 PowerShell New-WebServiceProxy commandlet 来获得与 WebService(WCF/SOAP) 的连接。可以连接到 WebService,但是当我想执行一个方法时,我被拒绝访问。拒绝访问是因为 WebService 需要自定义消息头。但这对于 New-WebServiceProxy 是不可能的。

问题:连接/使用 WebService 并添加消息头的最简单方法是什么?是否有 PowerShell 示例脚本?(在这种情况下,我首选的语言是 PowerShell)

顺便说一句:是的,我知道有一个问题与我的非常相似:使用 New-WebServiceProxy 在 PowerShell 中添加自定义 SOAP 标头

先感谢您!

4

1 回答 1

2

这更像是一种解决方法,但也许它对您有用。不使用 cmdlet,而是创建一个 C# 或 VB.NET 项目,添加 WCF 服务引用,因为它打算被使用。然后创建一个类,该类为您要调用的每个服务方法提供一个方法,并公开您需要在 PowerShell 中使用的参数。

class MyProxy
{
    public string Url { get; set; }
    public string SomeParameterForTheHeader { get; set; }

    public string CallSomeMethod(string input1, string input2)
    {
        // create WCF context using this.Url
        // create MessageHeader using this.SomeParameterForTheHeader and add it to the context
        // call SomeMethod on the context using input1 and input2
    }
}

编译它并在 PowerShell 脚本中使用程序集和类。

[System.Reflection.Assembly]::LoadWithPartialName("MyAssembly") > $null
$ref = New-Object MyNamespace.MyProxy()
$ref.Url = "http://..."
$ref.SomeParameterForTheHeader = "your value here"
$ref.CallSomeMethod("input1", "input2")
于 2012-12-12T09:39:08.837 回答