-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwcf.ps1
193 lines (166 loc) · 6.82 KB
/
wcf.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<#
.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,
[Parameter(Position=3, Mandatory=$false)][Nullable[int]] $Timeout = $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;
}
if ($Timeout -ne $null) {
$Binding.SendTimeout = (New-TimeSpan -Seconds $Timeout);
}
return New-Object $proxyType($Binding, $EndpointAddress);
}