A while back I showed how to call a Web Service from PowerShell. It was pretty easy. Doing the same for a Windows Communication Foundation service is just about as easy. The only wrinkle is in specifying the binding and the endpoint address. WCF steers folks towards specifying this binding/endpoint address data in their application’s configuration file. However it just doesn’t seem right to create a config file for PowerShell.exe. Fortunately it is pretty easy to specify this information programmatically in PowerShell script.
The following is using the self-hosted WCF service app that you get when you install the latest Windows SDK. After you have installed .NET FX 3.0 runtime and the Windows SDK, extract the WCFSamples.zip file in place. The ZIP file is located in:
C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples
Then open this solution:
C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\TechnologySamples\Basic\Service\Hosting\SelfHost\CS\SelfHost.sln
and set the Service project as the startup project and then run the service.
Now open up the PowerShell prompt. Before doing anything else you will need to have the environment configured to compile using the C# 2.0 compiler. See my blog post on how to configure the VS 2005 specific variables in PowerShell. Another alternative (and probably better) is to use Lee Holme’s script to execute the standard vsvars80.bat file in a way that sets those environment variables in the PowerShell variable space.
Once you have the VS 2005 variables configured, execute the following commands from the directories indicated. Note: on 7# you may need to execute it twice if it fails the first time usually because of the Windows firewall. I had to unblock it via Windows Live One Care and execute that command again to get it to work.
C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples
Then open this solution:
C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\TechnologySamples\Basic\Service\Hosting\SelfHost\CS\SelfHost.sln
and set the Service project as the startup project and then run the service.
Now open up the PowerShell prompt. Before doing anything else you will need to have the environment configured to compile using the C# 2.0 compiler. See my blog post on how to configure the VS 2005 specific variables in PowerShell. Another alternative (and probably better) is to use Lee Holme’s script to execute the standard vsvars80.bat file in a way that sets those environment variables in the PowerShell variable space.
Once you have the VS 2005 variables configured, execute the following commands from the directories indicated. Note: on 7# you may need to execute it twice if it fails the first time usually because of the Windows firewall. I had to unblock it via Windows Live One Care and execute that command again to get it to work.
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
7# .\svcutil.exe http://localhost:8000/ServiceModelSamples/service?wsdl
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.30]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from ‘http://localhost:8000/ServiceModelSamples/
service?wsdl’ using WS-Metadata Exchange or DISCO.
Generating files…
C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\CalculatorService.cs
C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\output.config
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
10# csc /t:library CalculatorService.cs /r:"C:\Program Files\Reference Assemblie
s\Microsoft\Framework\v3.0\System.ServiceModel.dll"
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.
7# .\svcutil.exe http://localhost:8000/ServiceModelSamples/service?wsdl
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.30]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from ‘http://localhost:8000/ServiceModelSamples/
service?wsdl’ using WS-Metadata Exchange or DISCO.
Generating files…
C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\CalculatorService.cs
C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\output.config
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
10# csc /t:library CalculatorService.cs /r:"C:\Program Files\Reference Assemblie
s\Microsoft\Framework\v3.0\System.ServiceModel.dll"
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
28# [Reflection.Assembly]::LoadWithPartialName("System.ServiceModel")
GAC Version Location
— ——- ——–
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0…
GAC Version Location
— ——- ——–
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0…
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
32# [Reflection.Assembly]::LoadFrom("$pwd\CalculatorService.dll")
GAC Version Location
— ——- ——–
False v2.0.50727 C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\Calcu…
32# [Reflection.Assembly]::LoadFrom("$pwd\CalculatorService.dll")
GAC Version Location
— ——- ——–
False v2.0.50727 C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\Calcu…
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
47# $wsHttpBinding = new-object System.ServiceModel.WSHttpBinding
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
48# $endpoint = new-object System.ServiceModel.EndpointAddress("http://localhost
:8000/ServiceModelSamples/service")
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
61# $calcService = new-object CalculatorClient($wsHttpBinding, $endpoint)
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
63# $calcService.Add(3,4)
7
47# $wsHttpBinding = new-object System.ServiceModel.WSHttpBinding
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
48# $endpoint = new-object System.ServiceModel.EndpointAddress("http://localhost
:8000/ServiceModelSamples/service")
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
61# $calcService = new-object CalculatorClient($wsHttpBinding, $endpoint)
[C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin]
63# $calcService.Add(3,4)
7
OK there it is. Nothing too exciting in terms of the results but hey we have PowerShell working with .NET 3.0, specifically Windows Communication Foundation. Pretty cool!
This is actually pretty cool and could be useful.
hi.. its very useful for me..
good work..
61# $calcService = new-object CalculatorClient($wsHttpBinding, $endpoint)..
what is this CalculatorClient? any method in wcf or anthing else?
Is it possible to specify the SendTimeOut. It looks like my powershell close my service connection after 1 minute.
There should be a Timeout property on the proxy class that gets created. I know there is if you use the New-WebServiceProxy cmdlet in PowerShell 2.0.
I checked and it seemed like a get property only on the proxy. Do I have it wrong?
Johnnie, You can use the -TimeOutFlag of the Get-WcfProxy powershell function here http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/
That is based on code originally written by Sharepoint MVP Christian Glessner and described here http://www.ilovesharepoint.com/2008/12/call-wcf-services-with-powershell.html?showComment=1328948458410#c8849419888666968020
CalculatorClient is the proxy class created by the svcutil utility based on the WSDL from the CalculatorService on my local machine. It was one of the WCF samples IIRC. BTW if you are using PowerShell 2.0, the above functionality is replaced by the cmdlet New-WebServiceProxy.