Calling a WCF Service from PowerShell

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 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\Bin]
7# .\svcutil.exe
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
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…
[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…
[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(“

[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)
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!
About these ads
This entry was posted in PowerShell. Bookmark the permalink.

7 Responses to Calling a WCF Service from PowerShell

  1. Unknown says:

    This is actually pretty cool and could be useful.

  2. satheesh says:

    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?

  3. Johnnie says:

    Is it possible to specify the SendTimeOut. It looks like my powershell close my service connection after 1 minute.

  4. rkeithhill says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s