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

23 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.

Sarah Urmeneta said...
This comment has been removed by the author.
Anonymous said...

How do you pass a complex type parameter to the service operation?

replica chanel handbags said...

I dropped a quick note here on the blog to let you all know of this bag’s availability online, and then I’m off to the meetings here in rainy Milan before Fashion Week in the city officially begins tomorrow.

سما احمد said...

تنظيف خزانات بمكة
تنظيف شقق بالدمام
تنظيف منازل بالدمام
تسليك مجاري بالدمام
رش مبيدات بالدمام
كشف تسربات المياه بالدمام





شركة تنظيف مسابح بالرياض
شركة تنظيف موكيت بالرياض
شركة تنظيف مجالس شرق الرياض
شركة رش مبيدات بالرياض
شركة تنظيف خزانات شرق الرياض
شركة الصفرات لمكافحة الحشرات بالرياض
شركة تنظيف شقق شرق الرياض
شركة نقل اثاث شرق الرياض
شركة تنظيف بيوت بالرياض
شركة تنظيف منازل شرق الرياض
شركة الصفرات لتنظيف الفلل
شركة نقل عفش بالرياض
شركة تنظيف منازل بالرياض
شركة مكافحة النمل الابيض بالرياض
شركة تنظيف موكيت بالخرج
شركة عزل خزانات بالرياض
شركة تنظيف خزانات بالمدينة المنورة

سما احمد said...

شركة تنظيف بالرياض
شركة جلي بلاط بالرياض
شركة تسليك مجارى بالرياض
تنظيف ستائر بالرياض


شركة تنظيف مساجد بالرياض
شركة كشف تسربات المياه بالرياض
تنظيف أنابيب الصرف الصحي
شركة تنظيف بالبخار
شركة تنظيف رخام بالرياض
شركة تنظيف زجاج الابراج

شركة تنظيف بيوت الشعر بالرياض



شركة الصفوة

ديكورات غرف نوم
تنظيف خزانات بالدمام
تنظيف خزانات بجدة

سما احمد said...

شركة مكافحة حشرات بالرياض

شركة شراء الاثاث مستعمل بالرياض
شركة تنظيف شمال الرياض
شركة تنظيف شرق الرياض
شركة عزل اسطح بالرياض
شركة تنظيف شقق بالرياض
شركة تنظيف موكيت بالرياض
شركة تنظيف مجالس بالرياض
شركة تنظيف منازل بالرياض
شركة تنظيف بالرياض
شركة تنظيف فلل بالرياض
شركة عزل خزانات غرب الرياض
شركة جلي بلاط بالرياض
شركة تنظيف خزانات الرياض

سما احمد said...

شركة مكافحة النمل الابيض بالرياض
شركة تنظيف واجهات حجر بالرياض
شركة تخزين عفش بالرياض
شركة تنظيف مسابح بالرياض
شركة تنظيف واجهات زجاج بالرياض
شركة نقل اثاث بالرياض
شركة تنظيف خزانات بالخرج

Kumar Vikal said...

Happy Ganesh Chaturthi SMS
Ganesh Chaturthi SMS in Hindi
Ganesh Chaturthi SMS in Marathi, Hindi
Ganesh Chaturthi in Marathi Language, SMS, Wishes, Quotes, Messages
Ganpati Bappa SMS in Hindi, Marathi, Ganesh Chaturthi SMS, Ganpati Bappa Quotes
Salman Khan Upcoming Movies
Android M Features
Xperia Z5 Specifications
Sultan Star Cast
Uncoming Hindi Movies 2016

Kumar Vikal said...

Gandhi Jayanti SMS
Happy Gandhi Jayanti
Gandhi Jayanti Images

Kumar Vikal said...

Happy Gurpurab
Guru Nanak Jayanti Images
Guru Nanak Jayanti
26 January Images
Hate Story 3 Zarine Khan
Hate Story 3 Hot Zarine Khan
Hot Zarine Khan in Hate Story 3

Hug Day said...

happy hug day Images
hug day 2016 Wishes cards greetings
Hug day Gift Ideas For Him/Her
hug day sms for bf

Pervez Joarder said...

I am thus content you recently understand this excellent. When I severely preferred this data as well the subsequent conflict. At this time sign on Payday Loans Appreciate it intended for good facts.

Libin Huang said...

20160426libinhollister clothing store
oakley sunglasses
michael kors handbags
tiffany outlet
ray-ban sunglasses
gucci outlet online
ray-ban sunglasses
burberry outlet online
coach outlet canada
ray-ban sunglasses
herve leger outlet
ray ban sunglasses sale
air max 90
oakley sunglasses
adidas wings shoes
michael kors outlet
michael kors factory outlet
louis vuitton bags
michael kors online outlet
nba jerseys
ralph lauren pas cher
oakley sunglasses
toms shoes
nike air max 90
nike tn pas cher
air max 90
lululemon outlet
nike air force 1
true religion outlet
michael kors outlet
burberry outlet store
ray ban sunglasses
kobe shoes
true religion outlet
discount michael kors handbags