Powershell: Error consuming WCF services with MTOM message encoding Powershell: Error consuming WCF services with MTOM message encoding powershell powershell

Powershell: Error consuming WCF services with MTOM message encoding


As of the time of this writing, I still have not been able to successfully use the New-WebServiceProxy cmdlet with a WCF service with MTOM enabled; it does not look like the cmdlet supports it. My workaround involved running svcutil.exe against the wsdl, and then compiling the class into a dll using csc.exe. I then loaded the generated assembly into the powershell run time, and then configured the endpoint and binding of the proxy class manually:

Generating the .cs file from your wsdl:

$svcUri = "http://yourdomain/yourService.svc?wsdl";$csFile = $className + '.cs';   # The name of the generated .cs file$dllName = [System.IO.Path]::Combine($temp, $className + ".dll")$svcUtilresult = svcutil.exe /noConfig /out:$csFile $svcUri

Note svcutil.exe and csc.exe may not be in your powershell's PATH. You can either add it to your PATH or use the full path. Svcutil can be found within your Microsoft SDKs\Windows\<version>\bin. csc.exe is located in your %windir%Microsoft .Net folder

Once you have generated the .cs file, you will need to compile it into a dll:

&"csc.exe" /t:library /out:$dllName $csFile

Load the compiled dll into powershell:

$fileStream = ([System.IO.FileInfo] (Get-Item ".\$dllName")).OpenRead()$dllBytes = new-object byte[] $fileStream.Length$fileStream.Read($dllBytes, 0, $fileStream.Length)$fileStream.Close()[System.Reflection.Assembly]::Load($dllBytes)

Instantiate the proxy client in powershell:

# Load System.ServiceModel, which can be found in your Framework\v3.0\Windows Communication Foundation folder[System.Reflection.Assembly]::LoadFile($pathToSystemServiceModel)# className is the name of your service$serviceClientName = $className + "Client"$basicHttpBinding = New-Object System.ServiceModel.BasicHttpBinding$basicHttpBinding.MessageEncoding = [System.ServiceModel.WSMessageEncoding]::Mtom$endPoint = New-Object System.ServiceModel.EndpointAddress($svcUri)$wsClient = New-Object $serviceClientname($basicHttpBinding, $endPoint)


I was having a similar problem. However I happened to already have the ClientBase generated code compiled into a local assembly.

My solution was:

add-type -path "..\..\bin\MYassemblyWithWCFCLient.dll"$binding = new-object system.servicemodel.basichttpbinding$binding.MessageEncoding = "Mtom"$endpoint = new-object System.ServiceModel.EndpointAddress("http://whodunit.oops/mtomandjerry.svc")$regProxy = new-object MySpecialNamespace.OopsServiceContractClient($binding, $endpoint)