- The PS2WCF script
- Meatdata discovery:
$wsdl = Get-WsdlImporter -wsdlUrl "net.tcp://localhost:8888/HelloService/mex" $wsdl.ImportAllEndpoints() | format-list
Output:
Note: For none mex endpoints use: Get-WsdlImporter -wsdlUrl http://anyurl/service.svc?wsdl –httpGet $true
- Generate the WCF proxy
$proxyTypes = Get-WcfProxy -wsdlUrl "net.tcp://localhost:8888/HelloService/mex"
or
$proxyTypes = Get-WcfProxy –wsdlImporter $wsdl
$proxyTypes | format-table
Output:
- Call a service operation
$address = New-Object System.ServiceModel.EndpointAddress("net.tcp://localhost:8888/HelloService/")
$binding = new-object System.ServiceModel.NetTcpBinding
$proxy = New-Object $proxyTypes -ArgumentList $binding, $address
$proxy.SayHello("Christian")
or use bindings and addresses generated from WsdlImporter
$endpoints = $wsdl.ImportAllEndpoints()
$proxy = New-Object $proxyTypes($endpoints[0].Binding, $endpoints[0].Address)
$proxy.SayHello("Christian")
Output:
By Christian Glessner
8 comments:
Very cool script. I've been meaning to put something like this together for a while. I'll put it to good use. :)
Merry Christmas!
I am new to the power shell. I just tried out the below example to get the returen values.
I am able to get the value of 'SayHello'. but how do I do it for 'SayHelloWithDataContract'
public interface IHello
{
[OperationContract(Action="SayHello")]
string SayHello(string name);
[OperationContract(Action = "SayHelloWithDataContract")]
string SayHelloWithDataContract(TestDataContract testDataContract);
}
[DataContract()]
public class TestDataContract
{
public TestDataContract(string FirstName1, string LastName1)
{
FirstName = FirstName1;
LastName = LastName1;
}
[DataMember()]
public string FirstName;
[DataMember()]
public string LastName;
}
regards,
kk
I did run into problems when using the above syntax that relies on implicit conversion of the $proxyTypes object:
$proxy = New-Object $proxyTypes -ArgumentList $binding, $address
Using the more explicit object properties for PowerShell 2.0solved the issue for me:
$proxy = New-Object $proxyTypes.FullName -ArgumentList $binding, $address
I just discovered you script. Thanks for writing it.
I have updated it to PowerShell version 2.0. Thanks a lot for writing it.
Also, I change the function Get-WcfProxy to Get-WcfProxyType and added a function called Get-WcfProxy that behaves like Get-WebServiceProxy.
What license is this code released under?
My powershell 2.0 version of the code: http://poshcode.org/3224
Thanks for creating the V2.0 :D
License: none. Just happy if you mention me.
Bye, Christian
Github for my fork here: https://github.com/justaprogrammer/PowerShellWCF/ A blog explaining my changes: http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/
Also not mentioned is I added a -Timeout flag to Get-WcfProxy
Hi Christian, Excellent little tool but I'm running into a small problem with it. It would seem I'm only able to call the following script once per session.
$wsdl = Get-WsdlImporter -wsdlUrl "http://localhost/TestService/Service.svc?wsdl" –httpGet $true
$proxyTypes = Get-WcfProxy –wsdlImporter $wsdl
$endpoints = $wsdl.ImportAllEndpoints()
$proxy = New-Object $proxyTypes($endpoints[0].Binding, $endpoints[0].Address)
$proxy.DoWork("Hello World")
It would seem to have something to do with the On-the-fly compilation of the proxy object.
Running that script the first time in a PS Console is fine. But if I run it a second time, I get
New-Object : Cannot find type [ServiceClient, 2hazj94y, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]: make sure the assembly containing this type is loaded.
At D:\Work\Sandbox\Powershell\CallWebService2.ps1:4 char:20
Any ideas ?
Thanks,
Eoin
Post a Comment