从powershell执行单向wcf服务操作

dev*_*ife 4 .net powershell wcf

我有一个计划任务,每小时执行一次powershell脚本.powershell脚本必须调用单向WCF服务操作.基本上它只需要启动一个操作.我的问题是我该怎么做呢?我认为只是执行url实际上会启动请求,但显然这是不正确的.

这是我想要做的:

$request = [System.Net.WebRequest]::Create("http://myserver.com/myservice/dosomething")
$request.GetResponse()
Run Code Online (Sandbox Code Playgroud)

该操作不接受任何参数并返回void.

Kei*_*ill 8

PowerShell 2.0使用New-WebServiceProxy cmdlet实现这一点:例如:

$zip = New-WebServiceProxy -uri http://www.webservicex.net/uszip.asmx?WSDL
$zip.getinfobyzip(20500).table

CITY      : Washington
STATE     : DC
ZIP       : 20500
AREA_CODE : 202
TIME_ZONE : E
Run Code Online (Sandbox Code Playgroud)


jdm*_*hal 7

我认为问题是你拥有的代码实际上是创建一个HttpWebRequest,而不是一个WCF请求.(换句话说,它只是在URL上执行HTTP GET请求,没有SOAP或.NET Remoting信息.)

您应该能够按照这些说明创建适当的端点:

http://msdn.microsoft.com/en-us/magazine/cc163647.aspx#S11

它应该看起来像这样:

$httpBinding = New-Object System.ServiceModel.BasicHttpBinding
$endpointAddress = New-Object System.ServiceModel.EndpointAddress 'http://myserver.com/myservice/dosomething'
$contractDescription = [System.ServiceModel.Description.ContractDescription]::GetContract([IYourInterface], $httpBinding, $endpointAddress)
$serviceEndpoint = New-Object System.ServiceModel.Description.ServiceEndpoint $contractDescription
$channelFactory = New-Object "System.ServiceModel.ChannelFactory``1[IYourInterface]" $serviceEndpoint
$webProxy = $channelFactory.CreateChannel();
$webProxy.yourServiceMethod();
Run Code Online (Sandbox Code Playgroud)

请注意,您需要导入具有IYourInterface此类的DLL 才能工作:

[void] [Reflection.Assembly]::LoadFrom('path/to/your.dll')
Run Code Online (Sandbox Code Playgroud)

或者,如果您为服务定义了WSDL,则可以按照这些更简单的说明访问该服务:

http://blogs.technet.com/heyscriptingguy/archive/2009/11/17/hey-scripting-guy-november-17-2009.aspx

或者,您可以找出HTTP SOAP请求需要的样子,并在其中自己构建HttpWebRequest.