PowerShell和Web服务 - 添加标头

时间:2012-12-11 16:59:31

标签: wcf web-services powershell soap

我使用PowerShell New-WebServiceProxy命令行开关来获取与WebService(WCF / SOAP)的连接。可以连接到WebService,但是当我想执行一个方法时,我得到一个拒绝访问。拒绝访问是因为WebService需要自定义邮件头。但是使用New-WebServiceProxy不可能实现这一点。

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

顺便说一句:是的,我知道有一个问题与我的问题非常相似:Add custom SOAP header in PowerShell using New-WebServiceProxy

提前谢谢!

1 个答案:

答案 0 :(得分:2)

这更像是一种解决方法,但它可能适合您。不要使用cmdlet,而是创建一个C#oder 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")
相关问题