Powershell New-WebServiceProxy - how to call WCF service

丶灬走出姿态 提交于 2021-01-29 07:04:47

问题


I'm trying to call a WCF service from PowerShell.

This is what I have so far based on a few misc example I found on the web:

# Create the WebSvcURL variable and pass the WSDL URL
$WebSvcURL= “http://localhost/DEMO/SetPassKey/SetPassKey_Logic_SetPassKeyWebService_SetPassKeyWCF.svc?wsdl“

#Create the Web Service Proxy Object

#$serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -Namespace "http://Sample.SetPassKey.Logic" -Class Program -UseDefaultCredential
$serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -UseDefaultCredential

# Create Request Object
$namespace = $serviceProxy.getType().namespace
write-host "Namespace=$namespace"
$req  = New-Object ($namespace + "/SetPassKeyOrchWebRequest")  
$resp = New-Object ($namespace + "/SetPassKeyOrchWebResponse")  

$req.NewPassKey = "TEST"    # <--- PUT YOUR NEW PASSWORD HERE 

$resp = $serviceProxy.SetPassKey($req) 

$resp 

I'm confused on a few things:

  1. If and when I need to include the -Namespace and -Class parameters on the. Since it looks at the WSDL, I don't understand why it needs the -Namespace and -Class.
  2. How to construct the request/response objects
  3. Should the URL be the actual URL of the web service or should it include the ?wsdl suffix

UPDATE: I found this blog which states:

The -Namespace parameter is optional and when not specified then it gets a random value from the cmdlet.

I was thinking it was an XML Namespace, but it's a .NET framework namespace.

# Create the WebSvcURL variable and pass the WSDL URL
$WebSvcURL= “http://localhost/DEMO/SetPassKey/SetPassKey_Logic_SetPassKeyWebService_SetPassKeyWCF.svc?wsdl“

#Create the Web Service Proxy Object

$serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -UseDefaultCredential -Namespace "MyNamespace" -Class Program 

# Create Request Object
$req  = New-Object ("MyNamespace.SetPassKeyOrchWebRequest")  
$resp = New-Object ("MyNamespace.SetPassKeyOrchWebResponse")  

$req.NewPassKey = "TEST"    # <--- PUT YOUR NEW PASSWORD HERE 

$resp = $serviceProxy.SetPassKey($req) 

$resp 

Now I get this error: Exception calling "SetPassKey" with "1" argument(s): "The underlying connection was closed: An unexpected error occurred on a receive."

The webservice can be called by a C# console program, so I know it works. Just need to call it from PowerShell.


回答1:


I tried some things, and finally got it to work. I did change my binding from WCF-WSHttp to WCF-BasicHttp.

I also followed this post, which said they only got it working by using the auto-generated namespace.

One of my issues originally was using "/" instead of "." as the separator between the namespace and the web request/response class names.

I'm still confused by the -namespace and -class parameters, as the post above said it only got the process working by using the autogenerated namespace (which happens when you omit the -namespace parm).

My code ended up something like this:

# Create the WebSvcURL variable and pass the WSDL URL
$WebSvcURL= “http://localhost/DEMO/SetPassKey/SetPassKey_Logic_SetPassKeyWebService_SetPassKeyWCF.svc?wsdl“

#Create the Web Service Proxy Object
 $serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -UseDefaultCredential

$autoGenNamespace = $serviceProxy.getType().namespace
write-host "Namespace=$namespace"
$req  = New-Object ($autoGenNamespace + ".SetPassKeyOrchWebRequest")  
$resp = New-Object ($autoGenNamespace + ".SetPassKeyOrchWebResponse")  

$req.NewPassKey = "TEST"    # <--- PUT YOUR NEW PASSWORD HERE 

$req.NewPassKey = "TEST"      # <--- PUT YOUR NEW PASSWORD HERE 
Write-Host "Request:"
Write-Host ($req | Format-Table | Out-String)

Write-Host "About to call WebService" 
$resp = $serviceProxy.SetPassKey($req) 


Write-Host "Response:"
Write-Host ($resp | Format-Table | Out-String)

NOTE: Just putting the variable name without the write-host statement was causing things to come out in a different sequence. The FormatTable was still truncating some fields, so I will list each return string in my $resp separately.



来源:https://stackoverflow.com/questions/65692957/powershell-new-webserviceproxy-how-to-call-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!