Last active
April 21, 2016 10:40
-
-
Save haraldfianbakken/9bd455be4b66916a06a6b6533ea819b2 to your computer and use it in GitHub Desktop.
Creating a custom proxy client method in Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Create-SVCProxies{ | |
[CmdletBinding()] | |
param([Parameter(Mandatory=$true)]$endpoint); | |
# If the endpoint name contians .svc , give it a more reasonable classname. | |
$className = $endpoint.Name -replace ".svc" , ""; | |
# WMF5 required (PowerShell 5) | |
$f = New-TemporaryFile; | |
$csFile = $f.FullName + $className+ '.cs'; # The name of the generated .cs file | |
$dllFile = $f.FullName + $className+ '.dll'; # The name of the generated .cs file | |
# Hardcoded path for example | |
$cmd = "$(${env:ProgramFiles(x86)})\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\SvcUtil.exe"; | |
if(-not (Get-Command $cmd)){ | |
Write-error "Unable to find svcutil!"; | |
throw "SVCUtil is missing!"; | |
} | |
if(-not (Get-Command csc.exe)){ | |
Write-error "Unable to find csc.xee"; | |
throw "csc.exe is missing!"; | |
} | |
Write-Verbose "Invoking $cmd /noConfig /out:$csFile $($endpoint.EndpointAddress)"; | |
.$cmd /noConfig /out:$csFile $($endpoint.EndpointAddress) | Out-Null | |
."csc.exe" /t:library /out:$dllFile $csFile | Out-Null | |
if($?){ | |
$ep|Add-Member -MemberType NoteProperty -Name "ProxyFile" -Value $dllFile; | |
$ep|Add-Member -MemberType NoteProperty -Name "ClassName" $className; | |
Write-Verbose "Proxies created and added to endpoint!" | |
} else { | |
Write-error "SVCUtil failed! $($LASTEXITCODE)"; | |
} | |
} | |
$endpoint = new-Object PSCustomObject -Property @{ | |
"Name"="SomeService.svc" | |
"EndpointAddress"="http://localhost/someservice.svc?wsdl"; | |
} | |
Create-SVCProxies -endpoint $endpoint -Verbose | |
# Now use the proxy that was just created (Assuming everything went well) | |
$assembly = Add-Type $endpoint.ProxyFile -PassThru; | |
# Try to figure what the client name was | |
$clientName = $assembly|? { $_.Name -match "Client"}}|Select Name | |
Write-Out "The CustomClientProxyName is $clientName" | |
# Use the client | |
$customBinding = New-Object System.ServiceModel.Channels.CustomBinding; | |
<# Example of WSHttpBinding with username | |
$binding = New-Object System.ServiceModel.WsHttpBinding; | |
$binding.Security.Mode = [System.ServiceModel.SecurityMode]::TransportWithMessageCredential; | |
$binding.Security.Message.ClientCredentialType = [System.ServiceModel.MessageCredentialType]::UserName | |
$binding.Security.Transport.ClientCredentialType = [System.ServiceModel.MessageCredentialType]::None; | |
#> | |
# Verify that the name of the file was actually | |
$proxy = New-Object $clientName($binding, (New-Object System.ServiceModel.EndpointAddress($url))) | |
$proxy.DoSomething(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment