Perform one-way wcf service from powershell

I have a scheduled task that runs a powershell script every hour. The power part of the script is to make a one-way WCF service operation call. Essentially, it just needs to start the operation. My question is: how do I do this? I thought that just executing the URL would actually start with the request, but apparently this is not true.

Here is what I tried to do:

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

The operation takes no parameters and returns void.

+3
source share
5 answers

PowerShell 2.0 makes this trivial with the New-WebServiceProxy cmdlet, for example:

$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
+7
source

, , , , HttpWebRequest, WCF. ( , HTTP- GET URL- 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();

, DLL IYourInterface:

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

, WSDL, , :

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

, , HTTP SOAP, HttpWebRequest.

+7

, HTTP, . , WebRequest POST, GET.

PowerShell, , :

$request = [System.Net.WebRequest]::Create("http://myserver.com/myservice/dosomething") 
$request.Method = "GET"
$request.GetResponse()

, !

0

HTTP-:

  • Fiddler,
  • WCF "" ( )
  • Invoke-WebRequest - XML SOAP , : Content-Type, SOAPAction , , .
  • (3) , SOAP.

WCF WCF, 3-4 HTTP. SOAP- . , Fiddler "" HTTP.

0

I like jdmichal's answer because it is intuitive to make it work for inputs where the parameters are objects ... the code required some changes, this is what worked for me:

[Reflection.Assembly]::LoadFrom("C:\.....\WcfService2.dll")
[Reflection.Assembly]::LoadWithPartialName("System.ServiceModel")

$httpBinding = New-Object System.ServiceModel.BasicHttpBinding
if($useNTLM){
    #passes the default creds
    $httpBinding.Security.Mode = "TransportCredentialOnly"
    $httpBinding.Security.Transport.ClientCredentialType = "Ntlm"
}
$endpointAddress = New-Object System.ServiceModel.EndpointAddress 'http://localhost:63600/Service1.svc'

$contractDescription = [System.ServiceModel.Description.ContractDescription]::GetContract([WcfService2.IService1])

$serviceEndpoint = New-Object System.ServiceModel.Description.ServiceEndpoint($contractDescription, $httpBinding, $endpointAddress)
$channelFactory = New-Object "System.ServiceModel.ChannelFactory``1[WcfService2.IService1]"($serviceEndpoint)

$webProxy = $channelFactory.CreateChannel();
$result = $webProxy.GetData(123);
Write-Output $result #prints 123
$comptype = new-object WcfService2.CompositeType
$comptype.BoolValue =$false
$comptype.StringValue = "whatever123zxcv"
$result = $webProxy.GetDataUsingDataContract($comptype);
Write-Output $result #prints whatever123zxcv

UPDATE: I learned how to use composite objects using the New-WebServiceProxy cmdlet (thanks to http://www.sqlmusings.com/2012/02/04/resolving-ssrs-and-powershell-new-webserviceproxy-namespace-issue/ )

$wsProxy = New-WebServiceProxy -uri http://localhost:63600/Service1.svc
$wsProxy.UseDefaultCredentials = $true
$namespace = $wsProxy.GetType().Namespace
$myct = New-Object "$namespace.CompositeType"
$myct.BoolValue = $true;
$myct.StringValue = "asdfasdg";

$wsProxy.GetDataUsingDataContract($myct)
$wsProxy.GetData(123,$true)
0
source

Source: https://habr.com/ru/post/1727298/


All Articles