2012-12-11 111 views
1

我使用PowerShell New-WebServiceProxy命令行程序获取与WebService(WCF/SOAP)的连接。可以连接到WebService,但是当我想要执行一个方法时,我会拒绝访问。访问被拒绝是因为WebService需要自定义消息头。但是这对于New-WebServiceProxy是不可能的。PowerShell和Webservice - 添加标头

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

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

预先感谢您!

回答

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") 
+0

非常好的主意:-) – LaPhi