Execute a one-way wcf service operation from powershell

后端 未结 5 1739
情歌与酒
情歌与酒 2021-02-06 12:52

I have a scheduled task which executes a powershell script every hour. The powershell script must make a call to a one-way WCF service operation. Essentially it just needs to

5条回答
  •  旧时难觅i
    2021-02-06 13:27

    I think the problem is that the code you have is actually creating an HttpWebRequest, not a WCF request. (In other words, it's simply executing an HTTP GET request on the URL, with no SOAP or .NET Remoting information.)

    You should be able to follow these instructions to create a proper endpoint:

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

    It should look something like this:

    $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();
    

    Note that you'll need to import the DLL with the IYourInterface class for this to work:

    [void] [Reflection.Assembly]::LoadFrom('path/to/your.dll')
    

    Alternatively, if you have a WSDL defined for the service, you can follow these much easier instructions to access the service:

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

    Alternatively alternatively, you can figure out what the HTTP SOAP request needs to look like, and form it yourself within the HttpWebRequest.

提交回复
热议问题