Using PowerShell to call a WCF Service

-- PowerShell 2012. 9. 6. 16:19
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
PowerShell lets you create web service proxies from WSDLs via the New-WebServiceProxy cmdlet. However, it only works for SOAP web services running on HTTP endpoints. If you have a WCF service using only non http protocols, such as NetTcp, you cannot use New-WebServiceProxy.

Now, I’ve created and consumed my fair share of web services. So when I began to use PowerShell, I quickly figured out this unfortunate fact. I always knew that I could make use of WCF API to generate the proxies, but I never bothered to figure out how. The annoyance was pretty academic to me. At some point I event told someone on stackoverflow that it can’t be done.

Then on Friday, I accidentally discovered that a Sharepoint MVP by the name of Christian Glessner solved this problem in 2008 while visiting the excellent site poshcode.org. Christian explains his solution in detail on his blog.

His solution was a very elegant PowerShell 1.0 solution. However, I felt it could be improved by adding some PowerShell 2.0 features such as parameter collections. I also was really intrigued and wanted to dig into his solution. He was supportive of my initial modifications so I put the git repo of my changes on the justaprogrammer github organization.

Using my version

My version of the script creates three functions:

- Get-WsdlImporter
- Get-WcfProxyType
- Get-WcfProxy

The function that is most analogous to New-WebServiceProxy is Get-WcfProxy. I kept the name from Christian’s version of the code, despite the fact that New-WcfProxy would be more appropriate. In Christian’s version of the code, Get-WcfProxy only returned a System.Type of the generated proxy, not an instance of it. I renamed that to Get-WcfProxyType. Finally. Get-WsdlImporter takes a wsdl or mex endpoint and returns an instance of a System.ServiceModel.Description.WsdlImporter that represents that metadata.

By default Get-WsdlImporter tries to generate the WsdlImporter via metadata exchange, but it can parse a wsdl with the -HttpGet switch. Below are some examples illustrating its usage:

$wsdlImporter = Get-WsdlImporter 'http://localhost:14232/EchoService.svc/mex' # Mex endpoint
Get-WsdlImporter 'http://localhost:14232/EchoService.svc' -HttpGet # WDSL endpoint
Get-WsdlImporter 'http://localhost:14232/EchoService.svc?wsdl' -HttpGet # WSD: endpoint

I don’t see much point for calling Get-WcfProxyType directly so I will not illustrate how to use it here. Get-WcfProxy is the function you want to call. Its main parameter is either a url or a WsdlImporter. You can either let Get-WcfProxy pick the first endpoint it finds in the WsdlImporter, or specify an endpoint and url you want to use as parameters as illustrated below:.

$wsdlImporter = Get-WsdlImporter "http://localhost:14232/EchoService.svc/mex"
$proxy = Get-WcfProxy $wsdlImporter # using a WsdlImporter object
$proxy = Get-WcfProxy "http://$($hostname):14232/EchoService.svc/mex" # using a url
$proxy = Get-WcfProxy $wsdlImporter "http://localhost:14232/EchoService.svc/WCF" (New-Object System.ServiceModel.WSHttpBinding) # using a WsdlImporter and specifying the endpoint and binding.
$proxy = Get-WcfProxy 'net.tcp://localhost:8732/EchoService/mex' 'net.tcp://localhost:8732/EchoService/' (New-Object System.ServiceModel.NetTcpBinding) # using a metadata url and specifying the endpoint and binding.
$proxy.Echo("Justin Dearing"); # calling a service function

As you might have guessed from my examples I used the justaprogrammer EchoService in my tests.

The code itself

I recommend you refer to the github repo for the latest version of the code. However, the version I used here is posted on poshcode.org and embedded below.

<#  
.SYNOPSIS  
        Functions to call WCF Services With PowerShell.
.NOTES
        Version 1.2 11.02.2012
        Requires Powershell v2 and .NET 3.5
       
        Copyright (c) 2008 Christian Glessner
        Copyright (c) 2012 Justin Dearing
 
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to
        deal in the Software without restriction, including without limitation the
        rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
        sell copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
 
        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.
 
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
        FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
        IN THE SOFTWARE.
 
        Original version by Christian Glessner
        Blog: http://www.iLoveSharePoint.com
        Twitter: http://twitter.com/cglessner
        Codeplex: http://codeplex.com/iLoveSharePoint
       
        PowerShell v2.0 modification by Justin Dearing
        Blog: http://justaprogrammer.net
        Twitter: http://twitter.com/zippy1981
.LINK  
        Blog describing original version: http://www.ilovesharepoint.com/2008/12/call-wcf-services-with-powershell.html
        Authoritative version of this fork: https://github.com/justaprogrammer/PowerShellWCF
        Posted to PoshCode.org http://poshcode.org/?lang=&q=PS2WCF
#>
 
# load WCF assemblies
Add-Type -AssemblyName "System.ServiceModel"
Add-Type -AssemblyName "System.Runtime.Serialization"
 
<#  
.SYNOPSIS  
        Get metadata of a service
 
.DESCRIPTION  
        Parses a wsdl or mex and generates a WsdlImporter object from it.
.EXAMPLE
        Get-WsdlImporter 'http://localhost.fiddler:14232/EchoService.svc/mex'
.EXAMPLE
        Get-WsdlImporter 'http://localhost.fiddler:14232/EchoService.svc' -HttpGet
.EXAMPLE
        Get-WsdlImporter 'http://localhost.fiddler:14232/EchoService.svc?wsdl' -HttpGet
 
#>
function global:Get-WsdlImporter([CmdletBinding()][Parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$WsdlUrl, [switch]$HttpGet)
{
        if($HttpGet)
        {
                $local:mode = [System.ServiceModel.Description.MetadataExchangeClientMode]::HttpGet
        }
        else
        {
                $local:mode = [System.ServiceModel.Description.MetadataExchangeClientMode]::MetadataExchange
        }
       
        $mexClient = New-Object System.ServiceModel.Description.MetadataExchangeClient([Uri]$WsdlUrl, $mode);
        $mexClient.MaximumResolvedReferences = [System.Int32]::MaxValue
        $metadataSet = $mexClient.GetMetadata()
        $wsdlImporter = New-Object System.ServiceModel.Description.WsdlImporter($metadataSet)
       
        return $wsdlImporter   
}
 
<#  
.SYNOPSIS  
    Generate wcf proxy types
 
.DESCRIPTION  
    Examines a web services meta data (wsdl or mex) and generates the types for the client proxy,
        as well as request and response contracts.
.EXAMPLE  
    $proxyType = Get-WcfProxyType $wsdlImporter
        $endpoints = $wsdlImporter.ImportAllEndpoints();
        $proxy = New-Object $proxyType($endpoints[0].Binding, $endpoints[0].Address);
#>
function global:Get-WcfProxyType(
        [CmdletBinding()]
        [Parameter(ParameterSetName='WsdlImporter', Position=0, Mandatory=$true, ValueFromPipeline=$true)][ServiceModel.Description.WsdlImporter] $WsdlImporter,
        [Parameter(ParameterSetName='WsdlUrl', Position=0, Mandatory=$true, ValueFromPipeline=$true)][string] $WsdlUrl,
        [string] $proxyPath
) {
        switch ($PsCmdlet.ParameterSetName)
        {
                "WsdlUrl" {
                        $WsdlImporter = Get-WsdlImporter $WsdlUrl
                        trap [Exception]
                        {
                                $WsdlImporter = Get-WsdlImporter $WsdlUrl -HttpGet
                                continue
                        }
                        break
                }
                "WsdlImporter" { break }
        }
       
        $generator = new-object System.ServiceModel.Description.ServiceContractGenerator
       
        foreach($contractDescription in $wsdlImporter.ImportAllContracts())
        {
                [void]$generator.GenerateServiceContractType($contractDescription)
        }
       
        $parameters = New-Object System.CodeDom.Compiler.CompilerParameters
        if($proxyPath -eq $null)
        {
                $parameters.GenerateInMemory = $true
        }
        else
        {
                $parameters.OutputAssembly = $proxyPath
        }
       
        $providerOptions = New-Object "Collections.Generic.Dictionary[String,String]"
        [void]$providerOptions.Add("CompilerVersion","v3.5")
       
        $compiler = New-Object Microsoft.CSharp.CSharpCodeProvider($providerOptions)
        $result = $compiler.CompileAssemblyFromDom($parameters, $generator.TargetCompileUnit);
       
        if($result.Errors.Count -gt 0)
        {
                throw "Proxy generation failed"      
        }
       
        return $result.CompiledAssembly.GetTypes() | Where-Object {$_.BaseType.IsGenericType -and $_.BaseType.GetGenericTypeDefinition().FullName -eq "System.ServiceModel.ClientBase``1" }
}
 
<#  
.SYNOPSIS  
    Generate wcf proxy
 
.DESCRIPTION  
    Generate wcf proxy in a manner similar to a Get-WebServiceProxy
.EXAMPLE
    $proxy = Get-WcfProxy 'http://localhost.fiddler:14232/EchoService.svc/mex'
        $proxy.Echo("Justin Dearing");
.EXAMPLE
        $proxy = Get-WcfProxy 'net.tcp://localhost:8732/EchoService/mex' 'net.tcp://localhost:8732/EchoService/' (New-Object System.ServiceModel.NetTcpBinding)
        $proxy.Echo("Justin Dearing");
#>
function global:Get-WcfProxy(
        [CmdletBinding()]
        [Parameter(ParameterSetName='WsdlImporter', Position=0, Mandatory=$true, ValueFromPipeline=$true)][ServiceModel.Description.WsdlImporter] $WsdlImporter,
        [Parameter(ParameterSetName='WsdlUrl', Position=0, Mandatory=$true, ValueFromPipeline=$true)][string] $WsdlUrl,
        [Parameter(Position=1, Mandatory=$false)][string] $EndpointAddress = $null,
        [Parameter(Position=2, Mandatory=$false)][System.ServiceModel.Channels.Binding] $Binding = $null
) {
        if ($Binding -ne $null -and [string]::IsNullOrEmpty($EndpointAddress)) {
                throw New-Object ArgumentNullException '$EndPointAddress', 'You cannot set $Binding without setting $EndpointAddress.'
        }
       
        switch ($PsCmdlet.ParameterSetName)
        {
                "WsdlUrl" {
                        $WsdlImporter = Get-WsdlImporter $WsdlUrl
                        trap [Exception]
                        {
                                $WsdlImporter = Get-WsdlImporter $WsdlUrl -HttpGet
                                continue
                        }
                        break
                }
        }
       
        $proxyType = Get-WcfProxyType $wsdlImporter;
       
        if ([string]::IsNullOrEmpty($EndpointAddress)) {
                $endpoints = $WsdlImporter.ImportAllEndpoints();
                $Binding = $endpoints[0].Binding;
                $EndpointAddress = $endpoints[0].Address;
        }
       
        return New-Object $proxyType($Binding, $EndpointAddress);
}


출처 : http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/
posted by 어린왕자악꿍