使用MTOM消息编码使用WCF服务时出错



我目前正在探索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 = "应用程序/xop + xml";开始="&lthttp://tempuri.org/0>;边界= " uuid:
4001d529-32b9-4560-9f4b-550c35c67b03+id=4";start-info="text/xml"',但预期为"text/xml"。
请求失败,错误消息:

——uuid: 4001 d529 - 32 - b9 - 4560 - 9 - f4b - 550 c35c67b03 + id = 4
内容识别:&lthttp://tempuri.org/0>
Content-Transfer-Encoding: 8位
- type:应用程序/xop + xml; charset = utf - 8; type = " text/xml "
lts:信封xmlns: s = " http://schemas.xmlsoap.org/soap/envelope/"祝辞
lts: Body>
&ltTestWebServiceConnectionResponse xmlns = " http://myserver.com/"祝辞
&ltTestWebServiceConnectionResult&gtsuccess<TestWebServiceConnectionResult>
& lt/TestWebServiceConnectionResponse>
& lt/s: Body>
& lt/s: Envelope>
——uuid: 4001 d529 - 32 - b9 - 4560 - 9 - f4b - 550 c35c67b03 + id = 4—
——"
在第一行:1 char:38
+ proxyObject美元。TestWebServiceConnection <lt<() >gt error.txt
+ CategoryInfo: NotSpecified: (:) [], MethodInvocationException
+ fulllyqualifiederrid: DotNetMethodException

我可以通过其他客户端甚至微软提供的wcfclient工具来使用WCF服务。您可以看到,TestWebServiceConnectionResult返回success,但是代理对象似乎无法解析响应。

:

&ltserviceBehaviors&gt
&ltbehavior name="MyServiceBehavior">
&ltserviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100"/>
&ltserviceMetadata httpGetEnabled="true" httpGetEnabled=" false"/>
&ltserviceDebug includeExceptionDetailInFaults="false"/>
& lt/behavior>
& lt/serviceBehaviors>

绑定(我已经排除了超时值/读取器配额和消息大小,因为它们的值的排列似乎与我的问题无关):


&ltbasicHttpBinding&gt
&ltbinding name="basicHttpEndpointBinding" messageEncoding="Mtom">
&ltsecurity mode="None">
&lttransport clientCredentialType="None"/>
& lt/security>
& lt/basicHttpBinding>

&ltservice behaviorConfiguration="MyServiceBehavior" name="MyService. behavior "AccessService"比
&ltendpoint address=" binding="basicHttpBinding" bindingConfiguration="basicHttpEndpointBinding" name="basicHttpEndpointAccessService" bindingNamespace="http://myserver.com/" contract="MyService. net "IAccessService "/比
&ltendpoint address="mex" binding="basicHttpBinding" bindingConfiguration=" basichttppointbinding " name="mexEndpointAccess" contract="IMetadataExchange"/>
& lt/service>

在撰写本文时,我仍然无法成功地将New-WebServiceProxy cmlet与启用了MTOM的WCF服务一起使用;看起来cmlet不支持它。我的解决方法是针对wsdl运行svcutil.exe,然后使用csc.exe将类编译为dll。然后我将生成的程序集加载到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

注意 svcutil.execsc.exe可能不在你的powershell的PATH中。您可以将其添加到PATH中,也可以使用完整路径。Svcutil可以在您的Microsoft SDKsWindows<version>bin中找到。csc.exe位于您的%windir%Microsoft .Net文件夹

一旦你生成了。cs文件,你需要把它编译成一个dll:

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

加载编译后的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)

在powershell实例化代理客户端:

# Load System.ServiceModel, which can be found in your Frameworkv3.0Windows 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)

我也遇到过类似的问题。然而,我碰巧已经将ClientBase生成的代码编译成本地程序集。

我的解决方案是:

add-type -path "....binMYassemblyWithWCFCLient.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)

相关内容

  • 没有找到相关文章

最新更新