gpt4 book ai didi

wcf - Powershell:使用 MTOM 消息编码使用 WCF 服务时出错

转载 作者:行者123 更新时间:2023-12-02 21:11:07 27 4
gpt4 key购买 nike

我目前正在探索powershell的功能,但遇到了一个无法解决的问题。任何快速提示将不胜感激 =)

我的目标:从 powershell v2.0 调用 WCF 服务(使用 MTOM 消息编码配置)的方法(希望使用 new-webserviceproxy cmdlet)

我的问题:当消息编码设置为 Mtom 时,new-webserviceproxy cmdlet 无法正确解析服务的响应。我收到以下错误:

PowerShell:


$proxyObject = New-WebServiceProxy -URI "<a href="http://myserver.com/AccessService.svc?wsdl" rel="noreferrer noopener nofollow">http://myserver.com/AccessService.svc?wsdl</a>"<br/>
$proxyObject.TestWebServiceConnection()

Exception calling "TestWebServiceConnection" with "0" argument(s): "Client found response content type of 'multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="uuid:
4001d529-32b9-4560-9f4b-550c35c67b03+id=4";start-info="text/xml"', but expected 'text/xml'.
The request failed with the error message:
--
--uuid:4001d529-32b9-4560-9f4b-550c35c67b03+id=4
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/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>success</TestWebServiceConnectionResult>
</TestWebServiceConnectionResponse>
</s:Body>
</s:Envelope>
--uuid:4001d529-32b9-4560-9f4b-550c35c67b03+id=4--
--."
At line:1 char:38
+ $proxyObject.TestWebServiceConnection <<<< () >> error.txt
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

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

行为:

<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100"/>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>

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


<basicHttpBinding>
<binding name="basicHttpEndpointBinding" messageEncoding="Mtom">
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</basicHttpBinding>

服务

<service behaviorConfiguration="MyServiceBehavior" name="MyService.AccessService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpEndpointBinding" name="basicHttpEndpointAccessService" bindingNamespace="http://myserver.com/" contract="MyService.IAccessService"/>
<endpoint address="mex" binding="basicHttpBinding" bindingConfiguration="basicHttpEndpointBinding" name="mexEndpointAccess" contract="IMetadataExchange"/>
</service>

最佳答案

截至撰写本文时,我仍然无法成功使用 New-WebServiceProxy具有启用了 MTOM 的 WCF 服务的 cmdlet; cmdlet 看起来不支持它。我的解决方法包括运行 svcutil.exe针对 wsdl,然后使用 csc.exe 将类编译成 dll 。然后,我将生成的程序集加载到 powershell 运行时中,然后手动配置代理类的端点和绑定(bind):

从 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 中或使用完整路径。 Svcutil可以在您的 Microsoft SDKs\Windows\<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 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)

关于wcf - Powershell:使用 MTOM 消息编码使用 WCF 服务时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com