Keith's profileKeith Hill's BlogPhotosBlogListsMore Tools Help
    November 08

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

    [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("
    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!

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Keith Hill has turned off comments on this page.
    Picture of Anonymous
    (no name) wrote:
    This is actually pretty cool and could be useful.
    June 25

    Trackbacks

    Weblogs that reference this entry
    • None