从 WCF 客户端连接到服务器的问题 - HTTPS 端点 - 适用于本地开发但不适用于服务器
Issue connecting to Server from WCF Client - HTTPS endpoint - Works on Local Dev but not Server
更新日志:
- 添加了更多细节 - 包括 WCF 跟踪。
首先,没有很多 WCF 经验,如果这是一个明显的错误,我们深表歉意。我正在编写一个简单的控制台应用程序,它需要连接到第三方 SOAP Web 服务。供应商提供的WSDL在这里:
我在 Visual Studio 2010 年(使用 .NET 4.0)中使用它创建了一个 服务参考 来创建后面的自动生成代码。我还为各种配置设置了 app.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="CompanyCustomConfig"
type="Company.Common.CustomConfigSections.EncryptedSomethingQuiteStrangeCredentialsSection, Company.Common"
/>
<section name="CompanyMachineConfig"
type="Company.Common.CustomConfigSections.MachineEnvironmentKeySection, Company.Common"
/>
</configSections>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="sessionMethodsSoap"
receiveTimeout="00:10:00"
closeTimeout="00:10:00"
openTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="Transport" />
</binding>
<binding name="sessionMethodsSoap1" />
<binding name="recipientMethodsSoap">
<security mode="Transport">
<!--<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>-->
</security>
</binding>
<!--<binding name="recipientMethodsSoap1" />-->
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https:test.jsp"
binding="basicHttpBinding" bindingConfiguration="sessionMethodsSoap"
contract="Session.sessionMethodsSoap" name="sessionMethodsSoap" />
<endpoint address="https:test.jsp"
binding="basicHttpBinding" bindingConfiguration="recipientMethodsSoap"
contract="Recipient.recipientMethodsSoap" name="recipientMethodsSoap" />
</client>
</system.serviceModel>
<appSettings>
<add key="SomethingQuiteStrangeActionsClientBaseUrlWL0280" value="http://localhost.fiddler:13298/api/ua"/>
<add key="SomethingQuiteStrangeActionsContentTypeWL0280" value="application/json"/>
<add key="SomethingQuiteStrangeActionsClientBaseUrlTesting" value="http://localhost:13298/api/SomethingQuiteStrangeactions"/>
<add key="SomethingQuiteStrangeActionsContentTypeTesting" value="application/json"/>
<add key="SomethingQuiteStrangeDetailsClientBaseUrlLocalDevelopment" value="http://localhost.fiddler:13298/api/detailsofsomething/>
<add key="SomethingQuiteStrangeDetailsContentTypeLocalDevelopment" value="application/json"/>
<add key="SomethingQuiteStrangeDetailsClientBaseUrlTesting" value="http://localhost:13298/api/SomethingQuiteStrangedetails"/>
<add key="SomethingQuiteStrangeDetailsContentTypeTesting" value="application/json"/>
<add key="LogFileDirectoryTesting" value="C"/>
<add key="LogFileDirectoryLocalDevelopment" value="C:\LogFiles\CRM"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<CompanyCustomConfig>
<EncryptedSomethingQuiteStrangeCredentials>
<EncryptedSomethingQuiteStrangeCredential service="ThingLocalDevelopment" SomethingQuiteStrangeName="un" password="pw"/>
</EncryptedSomethingQuiteStrangeCredentials>
</CompanyCustomConfig>
<CompanyMachineConfig>
<MachineEnvironmentKeys>
<MachineEnvironmentKey name="DEV" environment="LocalDevelopment" />
<MachineEnvironmentKey name="SERVER" environment="Testing" />
</MachineEnvironmentKeys>
</CompanyMachineConfig>
</configuration>
现在,在我的 LocalDevelopment 盒子(即我的笔记本电脑)上,客户端工作正常,我可以使用他们的 Logon() 操作连接到第三方服务。当我将我的代码部署到我们的测试服务器时,相同的代码和配置会抛出以下异常:
Log File Started: 18/11/2015 20:11:25 on machine TESTSERVER
============================================================================ 18/11/2015 20:11 >>>> *** Application Logged: Date Logged: 18/11/2015
20:11:25 Message: Issue in Client (Client.Logon()) - communication
problem - Message: An error occurred while making the HTTP request to
test.jsp. This could be
due to the fact that the server certificate is not configured properly
with HTTP.SYS in the HTTPS case. This could also be caused by a
mismatch of the security binding between the client and the server.
Stack Trace:
Server stack trace: at
System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException
webException, HttpWebRequest request, HttpAbortReason abortReason)
at
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout) at
System.ServiceModel.Channels.RequestChannel.Request(Message message,
TimeSpan timeout) at
System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message
message, TimeSpan timeout) at
System.ServiceModel.Channels.ServiceChannel.Call(String action,
Boolean oneway, ProxyOperationRuntime operation, Object[] ins,
Object[] outs, TimeSpan timeout) at
System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation) at
System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
message)
Exception rethrown at [0]: at
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg) at
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type) at
Hachette.CRM.AdobeCRMService.Session.sessionMethodsSoap.Logon(LogonRequest
request) at Hachette.CRM.AdobeCRMService.AdobeClient.Logon() in
C:\My
Documents\Hachette.CRM\Hachette.CRM.AdobeCRMService\AdobeClient.cs:line
84other: System.Collections.ListDictionaryInternal
根据下面评论者的建议,我在发生错误的服务器上打开了 WCF 跟踪。在 通过通道
发送消息后抛出异常
Exception Type:
System.Net.WebException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message:
The underlying connection was closed: An unexpected error occurred on a send.
Exception Type:
System.IO.IOException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message:
Received an unexpected EOF or 0 bytes from the transport stream.
为清楚起见,这里有一张图片:
现在即使我们使用的是 HTTPS,第三方也不需要证书 - Logon() 操作为我提供了一个 sessionId 以供稍后使用 - 所以我假设使用 basicHttpBinding 我好吗?
要知道的事情:
- 我本地开发框是运行Windows8.1
- 我部署到的测试服务器是 运行 Windows Server2008 R2 Standard(已安装 SP1)。
- 我的客户端是一个 .NET 4.0 C# 控制台应用程序。
- 在出现故障的服务器上尝试了 SOAP UI,使用相同的 WSDL - 请求有效并且没有网络,所以它一定是 WCF/.NET 问题。
- 此外,是否需要为像我这样使用 WCF 的应用程序设置 WCF 激活 - 添加了服务引用和自动生成的代码?我们的已关闭,因为它导致其他站点崩溃(需要服务模型 3.0.0.0):
做一些 SSL 故障排除。部分选项:
快速尝试:有一个 Stack Overflow question with the same error message "Received an unexpected EOF or 0 bytes from the transport stream" 解决方案是在调用之前显式设置 SSL 版本:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Enable the System.Net
trace 这可能会给您进一步的线索。
使用 Wireshark 或类似于 sniff the SSL handshake。
最后的问题是我需要在 Windows 2008 Server R2 上安装 .NET 4.5.1。尽管我的目标是 .NET 4.0。这解决了问题,现在可以正常工作了。
更新日志:
- 添加了更多细节 - 包括 WCF 跟踪。
首先,没有很多 WCF 经验,如果这是一个明显的错误,我们深表歉意。我正在编写一个简单的控制台应用程序,它需要连接到第三方 SOAP Web 服务。供应商提供的WSDL在这里:
我在 Visual Studio 2010 年(使用 .NET 4.0)中使用它创建了一个 服务参考 来创建后面的自动生成代码。我还为各种配置设置了 app.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="CompanyCustomConfig"
type="Company.Common.CustomConfigSections.EncryptedSomethingQuiteStrangeCredentialsSection, Company.Common"
/>
<section name="CompanyMachineConfig"
type="Company.Common.CustomConfigSections.MachineEnvironmentKeySection, Company.Common"
/>
</configSections>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="sessionMethodsSoap"
receiveTimeout="00:10:00"
closeTimeout="00:10:00"
openTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="Transport" />
</binding>
<binding name="sessionMethodsSoap1" />
<binding name="recipientMethodsSoap">
<security mode="Transport">
<!--<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>-->
</security>
</binding>
<!--<binding name="recipientMethodsSoap1" />-->
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https:test.jsp"
binding="basicHttpBinding" bindingConfiguration="sessionMethodsSoap"
contract="Session.sessionMethodsSoap" name="sessionMethodsSoap" />
<endpoint address="https:test.jsp"
binding="basicHttpBinding" bindingConfiguration="recipientMethodsSoap"
contract="Recipient.recipientMethodsSoap" name="recipientMethodsSoap" />
</client>
</system.serviceModel>
<appSettings>
<add key="SomethingQuiteStrangeActionsClientBaseUrlWL0280" value="http://localhost.fiddler:13298/api/ua"/>
<add key="SomethingQuiteStrangeActionsContentTypeWL0280" value="application/json"/>
<add key="SomethingQuiteStrangeActionsClientBaseUrlTesting" value="http://localhost:13298/api/SomethingQuiteStrangeactions"/>
<add key="SomethingQuiteStrangeActionsContentTypeTesting" value="application/json"/>
<add key="SomethingQuiteStrangeDetailsClientBaseUrlLocalDevelopment" value="http://localhost.fiddler:13298/api/detailsofsomething/>
<add key="SomethingQuiteStrangeDetailsContentTypeLocalDevelopment" value="application/json"/>
<add key="SomethingQuiteStrangeDetailsClientBaseUrlTesting" value="http://localhost:13298/api/SomethingQuiteStrangedetails"/>
<add key="SomethingQuiteStrangeDetailsContentTypeTesting" value="application/json"/>
<add key="LogFileDirectoryTesting" value="C"/>
<add key="LogFileDirectoryLocalDevelopment" value="C:\LogFiles\CRM"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<CompanyCustomConfig>
<EncryptedSomethingQuiteStrangeCredentials>
<EncryptedSomethingQuiteStrangeCredential service="ThingLocalDevelopment" SomethingQuiteStrangeName="un" password="pw"/>
</EncryptedSomethingQuiteStrangeCredentials>
</CompanyCustomConfig>
<CompanyMachineConfig>
<MachineEnvironmentKeys>
<MachineEnvironmentKey name="DEV" environment="LocalDevelopment" />
<MachineEnvironmentKey name="SERVER" environment="Testing" />
</MachineEnvironmentKeys>
</CompanyMachineConfig>
</configuration>
现在,在我的 LocalDevelopment 盒子(即我的笔记本电脑)上,客户端工作正常,我可以使用他们的 Logon() 操作连接到第三方服务。当我将我的代码部署到我们的测试服务器时,相同的代码和配置会抛出以下异常:
Log File Started: 18/11/2015 20:11:25 on machine TESTSERVER ============================================================================ 18/11/2015 20:11 >>>> *** Application Logged: Date Logged: 18/11/2015 20:11:25 Message: Issue in Client (Client.Logon()) - communication problem - Message: An error occurred while making the HTTP request to test.jsp. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server. Stack Trace:
Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Hachette.CRM.AdobeCRMService.Session.sessionMethodsSoap.Logon(LogonRequest request) at Hachette.CRM.AdobeCRMService.AdobeClient.Logon() in C:\My Documents\Hachette.CRM\Hachette.CRM.AdobeCRMService\AdobeClient.cs:line 84other: System.Collections.ListDictionaryInternal
根据下面评论者的建议,我在发生错误的服务器上打开了 WCF 跟踪。在 通过通道
发送消息后抛出异常Exception Type:
System.Net.WebException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message:
The underlying connection was closed: An unexpected error occurred on a send.
Exception Type:
System.IO.IOException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message:
Received an unexpected EOF or 0 bytes from the transport stream.
为清楚起见,这里有一张图片:
现在即使我们使用的是 HTTPS,第三方也不需要证书 - Logon() 操作为我提供了一个 sessionId 以供稍后使用 - 所以我假设使用 basicHttpBinding 我好吗?
要知道的事情:
- 我本地开发框是运行Windows8.1
- 我部署到的测试服务器是 运行 Windows Server2008 R2 Standard(已安装 SP1)。
- 我的客户端是一个 .NET 4.0 C# 控制台应用程序。
- 在出现故障的服务器上尝试了 SOAP UI,使用相同的 WSDL - 请求有效并且没有网络,所以它一定是 WCF/.NET 问题。
- 此外,是否需要为像我这样使用 WCF 的应用程序设置 WCF 激活 - 添加了服务引用和自动生成的代码?我们的已关闭,因为它导致其他站点崩溃(需要服务模型 3.0.0.0):
做一些 SSL 故障排除。部分选项:
快速尝试:有一个 Stack Overflow question with the same error message "Received an unexpected EOF or 0 bytes from the transport stream" 解决方案是在调用之前显式设置 SSL 版本:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Enable the
System.Net
trace 这可能会给您进一步的线索。使用 Wireshark 或类似于 sniff the SSL handshake。
最后的问题是我需要在 Windows 2008 Server R2 上安装 .NET 4.5.1。尽管我的目标是 .NET 4.0。这解决了问题,现在可以正常工作了。