PoshCode Logo PowerShell Code Repository

PS2WCF by Justin Dearing 5 years ago (modification of post by Alexey view diff)
View followups from Justin Dearing | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3224"></script>download | new post

Call WCF Services with PowerShell using any binding. Generates proxy on the fly without needing any tool expect .NET 3.5. You can also discover the service endpoints, bindings and contracts. Read more on my blog: http://www.ilovesharepoint.com/2008/12/call-wcf-services-with-powershell.html
Rewritten to use PowerShell 2.0.

  1. <#  
  2. .SYNOPSIS  
  3.         Functions to call WCF Services With PowerShell.
  4. .NOTES
  5.         Version 1.1 10.02.2012
  6.         Requires Powershell v2 and .NET 3.5
  7.  
  8.         Original version by Christian Glessner
  9.         Blog: http://www.iLoveSharePoint.com
  10.         Twitter: http://twitter.com/cglessner
  11.         Codeplex: http://codeplex.com/iLoveSharePoint
  12.        
  13.         PowerShell v2.0 modification by Justin Dearing
  14.         Blog: http://justaprogrammer.net
  15.         Twitter: http://twitter.com/zippy1981
  16. .LINK  
  17.         Blog describing original version: http://www.ilovesharepoint.com/2008/12/call-wcf-services-with-powershell.html
  18.         Posted to PoshCode.org http://poshcode.org/?lang=&q=PS2WCF
  19. #>
  20.  
  21. # load WCF assemblies
  22. Add-Type -AssemblyName "System.ServiceModel"
  23. Add-Type -AssemblyName "System.Runtime.Serialization"
  24.  
  25. <#  
  26. .SYNOPSIS  
  27.         Get metadata of a service
  28.  
  29. .DESCRIPTION  
  30.         Parses a wsdl or mex and generates a WsdlImporter object from it.
  31. .EXAMPLE
  32.         Get-WsdlImporter 'http://localhost.fiddler:14232/EchoService.svc/mex'
  33. .EXAMPLE
  34.         Get-WsdlImporter 'http://localhost.fiddler:14232/EchoService.svc' -HttpGet
  35. .EXAMPLE
  36.         Get-WsdlImporter 'http://localhost.fiddler:14232/EchoService.svc?wsdl' -HttpGet
  37.  
  38. #>
  39. function global:Get-WsdlImporter([Parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$wsdlUrl, [switch]$httpGet)
  40. {
  41.         if($httpGet -eq $true)
  42.         {
  43.                 $local:mode = [System.ServiceModel.Description.MetadataExchangeClientMode]::HttpGet
  44.         }
  45.         else
  46.         {
  47.                 $local:mode = [System.ServiceModel.Description.MetadataExchangeClientMode]::MetadataExchange
  48.         }
  49.        
  50.         $mexClient = New-Object System.ServiceModel.Description.MetadataExchangeClient((New-Object System.Uri($wsdlUrl)),$mode)
  51.         $mexClient.MaximumResolvedReferences = [System.Int32]::MaxValue
  52.         $metadataSet = $mexClient.GetMetadata()
  53.         $wsdlImporter = New-Object System.ServiceModel.Description.WsdlImporter($metadataSet)
  54.        
  55.         return $wsdlImporter   
  56. }
  57.  
  58. <#  
  59. .SYNOPSIS  
  60.     Generate wcf proxy types
  61.  
  62. .DESCRIPTION  
  63.     Examines a web services meta data (wsdl or mex) and generates the types for the client proxy,
  64.         as well as request and response contracts.
  65. .EXAMPLE  
  66.     $proxyType = Get-WcfProxyType $wsdlImporter
  67.         $endpoints = $wsdlImporter.ImportAllEndpoints();
  68.         $proxy = New-Object $proxyType($endpoints[0].Binding, $endpoints[0].Address);
  69. #>
  70. function global:Get-WcfProxyType(
  71.         [Parameter(ParameterSetName='WsdlImporter', Position=0, Mandatory=$true, ValueFromPipeline=$true)][ServiceModel.Description.WsdlImporter] $wsdlImporter,
  72.         [Parameter(ParameterSetName='WsdlUrl', Position=0, Mandatory=$true, ValueFromPipeline=$true)][string] $wsdlUrl,
  73.         [string] $proxyPath
  74. ) {
  75.         switch ($PsCmdlet.ParameterSetName)
  76.         {
  77.                 "WsdlUrl" {
  78.                         $wsdlImporter = Get-WsdlImporter -wsdlUrl $wsdlUrl
  79.                         trap [Exception]
  80.                         {
  81.                                 $script:wsdlImporter = Get-WsdlImporter -wsdlUrl $wsdlUrl -httpGet $true
  82.                                 continue
  83.                         }
  84.                         break
  85.                 }
  86.                 "WsdlImporter" { break }
  87.         }
  88.        
  89.         $generator = new-object System.ServiceModel.Description.ServiceContractGenerator
  90.        
  91.         foreach($contractDescription in $wsdlImporter.ImportAllContracts())
  92.         {
  93.                 [void]$generator.GenerateServiceContractType($contractDescription)
  94.         }
  95.        
  96.         $parameters = New-Object System.CodeDom.Compiler.CompilerParameters
  97.         if($proxyPath -eq $null)
  98.         {
  99.                 $parameters.GenerateInMemory = $true
  100.         }
  101.         else
  102.         {
  103.                 $parameters.OutputAssembly = $proxyPath
  104.         }
  105.        
  106.         $providerOptions = New-Object "Collections.Generic.Dictionary[String,String]"
  107.         [void]$providerOptions.Add("CompilerVersion","v3.5")
  108.        
  109.         $compiler = New-Object Microsoft.CSharp.CSharpCodeProvider($providerOptions)
  110.         $result = $compiler.CompileAssemblyFromDom($parameters, $generator.TargetCompileUnit);
  111.        
  112.         if($result.Errors.Count -gt 0)
  113.         {
  114.                 throw "Proxy generation failed"      
  115.         }
  116.        
  117.         foreach($type in $result.CompiledAssembly.GetTypes())
  118.         {
  119.                 if($type.BaseType.IsGenericType)
  120.                 {
  121.                         if($type.BaseType.GetGenericTypeDefinition().FullName -eq "System.ServiceModel.ClientBase``1" )
  122.                         {
  123.                                 return $type
  124.                         }
  125.                 }
  126.         }
  127. }
  128.  
  129. <#  
  130. .SYNOPSIS  
  131.     Generate wcf proxy
  132.  
  133. .DESCRIPTION  
  134.     Generate wcf proxy in a manner similar to a Get-WebServiceProxy
  135. .EXAMPLE
  136.     $proxy = Get-WcfProxy 'http://localhost.fiddler:14232/EchoService.svc/mex'
  137.         $proxy.Echo("Justin Dearing");
  138. #>
  139. function global:Get-WcfProxy(
  140.         [Parameter(ParameterSetName='WsdlImporter', Position=0, Mandatory=$true, ValueFromPipeline=$true)][ServiceModel.Description.WsdlImporter] $wsdlImporter,
  141.         [Parameter(ParameterSetName='WsdlUrl', Position=0, Mandatory=$true, ValueFromPipeline=$true)][string] $wsdlUrl,
  142.         [string] $proxyPath
  143. ) {
  144.         switch ($PsCmdlet.ParameterSetName)
  145.         {
  146.                 "WsdlUrl" {
  147.                         $wsdlImporter = Get-WsdlImporter -wsdlUrl $wsdlUrl
  148.                         trap [Exception]
  149.                         {
  150.                                 $script:wsdlImporter = Get-WsdlImporter -wsdlUrl $wsdlUrl -httpGet $true
  151.                                 continue
  152.                         }
  153.                         break
  154.                 }
  155.         }
  156.   $proxyType = Get-WcfProxyType $wsdlImporter
  157.  
  158.   $endpoints = $wsdlImporter.ImportAllEndpoints();
  159.   return New-Object $proxyType($endpoints[0].Binding, $endpoints[0].Address);
  160. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me