Dec 22, 2008

Call WCF Services With PowerShell

Call WCF Services with PowerShell using any binding. Generates proxies on the fly without any tool expect .NET 3.5. You can also discover the service endpoints, bindings and contracts.
  • The PS2WCF script
 
Example:
Download sample WCF Hello Service project and start ConsoleShell
  • Meatdata discovery:

$wsdl = Get-WsdlImporter -wsdlUrl "net.tcp://localhost:8888/HelloService/mex" $wsdl.ImportAllEndpoints() | format-list

Output:

image

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:

image

  • 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:

image 

By Christian Glessner

10 comments:

Steven Murawski said...

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!

Anonymous said...

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

Anonymous said...

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

zippy1981 said...

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?

zippy1981 said...

My powershell 2.0 version of the code: http://poshcode.org/3224

Christian said...

Thanks for creating the V2.0 :D
License: none. Just happy if you mention me.
Bye, Christian

zippy1981 said...

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

Eoin said...

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

Anonymous said...


How do you use this nonWCF services which does not define MEX endpoints?
I also posted this in the PowerShell forum:

http://social.technet.microsoft.com/Forums/windowsserver/en-US/cc14dd1d-6210-458d-bd23-d59610f563f9/powershell-newwebserviceproxy-how-to-specify-bindings-for-a-wcf-service

digital signature said...

Congratulations, your blog is appealing and informative. Going through your Information, I found quite a few new ideas to implement.