Tun*_*ung 10 powershell wcf mtom
我目前正在探索powershell功能,但我遇到了一个我无法解决的问题.任何快速提示将不胜感激=)
我的目标:从powershell v2.0调用WCF服务(使用MTOM消息编码配置)中的方法(希望使用new-webserviceproxy cmdlet)
我的问题:当消息编码设置为Mtom时,new-webserviceproxy cmdlet无法正确解析服务的响应.我收到以下错误:
PowerShell:
$proxyObject = New-WebServiceProxy -URI "http://myserver.com/AccessService.svc?wsdl"
$proxyObject.TestWebServiceConnection()
使用"0"参数调用"TestWebServiceConnection"的异常:"客户端发现响应内容类型为'multipart/related; type ="application/xop + xml"; start ="<http://tempuri.org/0> "; boundary ="uuid:
4001d529-32b9-4560-9f4b-550c35c67b03 + id = 4"; start-info ="text/xml"',但预期'text/xml'.
请求失败并显示错误消息:
- -
--uuid:4001d529-32b9-4560-9f4b-550c35c67b03 + ID = 4
的Content-ID:<http://tempuri.org/0>
内容传送编码:8位
内容类型:应用/ XOP + xml的; charset = utf-8; type ="text/xml"
<s:Envelope xmlns:s ="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<TestWebServiceConnectionResponse xmlns ="http: //myserver.com /">
<TestWebServiceConnectionResult>成功</ TestWebServiceConnectionResult>
</ TestWebServiceConnectionResponse>
</ S:身体>
</秒:信封>
--uuid:4001d529-32b9-4560-9f4b-550c35c67b03 + ID = 4- -
- ."
在行:1 char:38
+ $ proxyObject.TestWebServiceConnection <<<<()>> error.txt
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:DotNetMethodException
注意我可以通过其他客户端甚至Microsoft提供的wcfclient工具来使用WCF服务.您可以看到TestWebServiceConnectionResult返回成功,但似乎代理对象无法解析响应.
行为:
<serviceBehaviors>
<行为名称= "MyServiceBehavior">
<serviceThrottling maxConcurrentCalls = "100" maxConcurrentSessions = "100"/>
<serviceMetadata httpGetEnabled = "真" httpsGetEnabled = "假"/>
<serviceDebug includeExceptionDetailInFaults = "假"/>
</行为>
</ serviceBehaviors>
绑定(我已经排除了超时值/读者配额和消息大小,因为它们的值的排列似乎与我的问题无关):
<basicHttpBinding>
<binding name ="basicHttpEndpointBinding"messageEncoding ="Mtom">
<security mode ="None">
<transport clientCredentialType ="None"/>
</ security>
</ basicHttpBinding>
服务
<服务behaviorConfiguration = "MyServiceBehavior"名称= "MyService.AccessService">
<端点地址= ""结合= "basicHttpBinding的" bindingConfiguration = "basicHttpEndpointBinding"名称= "basicHttpEndpointAccessService" bindingNamespace = "http://myserver.com/"合同= "MyService.IAccessService"/>
<endpoint address ="mex"binding ="basicHttpBinding"bindingConfiguration ="basicHttpEndpointBinding"name ="mexEndpointAccess"contract ="IMetadataExchange"/>
</ service>
截至撰写本文时,我仍无法将New-WebServiceProxycmdlet与启用MTOM的WCF服务一起成功使用;它看起来不像cmdlet支持它。我的解决方法涉及svcutil.exe针对wsdl 运行,然后使用将该类编译为dll csc.exe。然后,我将生成的程序集加载到powershell运行时,然后手动配置端点和代理类的绑定:
从wsdl生成.cs文件:
$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
Run Code Online (Sandbox Code Playgroud)
请注意, svcutil.exe并且csc.exe可能不在您的powershell的PATH中。您可以将其添加到PATH或使用完整路径。Svcutil可以在您的内找到Microsoft SDKs\Windows\<version>\bin。 csc.exe位于您的%windir%Microsoft .Net文件夹中
生成.cs文件后,您需要将其编译为dll:
&"csc.exe" /t:library /out:$dllName $csFile
Run Code Online (Sandbox Code Playgroud)
将编译的dll加载到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)
Run Code Online (Sandbox Code Playgroud)
在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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3350 次 |
| 最近记录: |