问题描述
|
我有一个在三个负载平衡的Web服务器上运行的Web服务,并且遇到了零星的错误。现在,我承认负载平衡部分可能有点像鲱鱼,但是当我仅使用1个Web服务器进行测试时,我无法重现该错误。如果我对所有三个Web服务器进行测试,都可以得到错误(但不是100%的时间,更可能是50%)。所有测试都是通过负载均衡器完成的,我们只是告诉负载均衡器我们要种植多少台服务器。
该代码是简单的单个请求代码。也就是说,没有状态。发出请求并返回响应。 Web服务代码是在IIS 7.5上运行的c#.NET 4。客户端代码既是网站又是桌面应用程序。
我得到两个例外之一:
System.ServiceModel.Security.MessageSecurityException:
不安全的或不正确的安全
从另一个接收到错误
派对。参见内部的FaultException
有关故障代码和详细信息。 ->
System.ServiceModel.FaultException:
安全上下文令牌已过期
或无效。消息不是
处理。
或者我得到:
System.ServiceModel.Security.SecurityNegotiationException:
安全通道无法打开
因为与
远程端点已失败。这可能
由于缺席或不正确
在指定的EndpointIdentity
EndpointAddress用于创建
渠道。请验证
指定或暗示的EndpointIdentity
通过EndpointAddress正确
标识远程端点。 ->
System.ServiceModel.FaultException:
安全令牌请求有
无效或格式错误的元素。
从您的.config文件的以下片段中可以看到,我没有使用安全性,因为它严格是内部Web服务。 (为了保护无辜者(即我),名字已更改)。
服务器端:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<configuration>
<!-- Service Side web.config -->
...
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled=\"true\" />
<services>
<service behaviorConfiguration=\"InternalUSEOnly.InternalUSEOnlyServiceBehavior\" name=\"InternalUSEOnly.InternalUSEOnlyService\">
<endpoint address=\"\" bindingNamespace=\"http://somecompany.com/webservices\" binding=\"wsHttpBinding\" contract=\"InternalUSEOnly.IInternalUSEOnlyService\">
<identity>
<dns value=\"localhost\" />
</identity>
</endpoint>
<endpoint address=\"mex\" binding=\"mexHttpBinding\" contract=\"IMetadataExchange\" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=\"InternalUSEOnly.InternalUSEOnlyServiceBehavior\">
<serviceMetadata httpGetEnabled=\"true\" />
<serviceDebug includeExceptionDetailInFaults=\"true\" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
...
</configuration>
客户端
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!-- Client Side web.config -->
<configuration>
...
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name=\"WSHttpBinding_IInternalUSEOnlyService\" closeTimeout=\"00:01:00\" openTimeout=\"00:01:00\" receiveTimeout=\"00:10:00\" sendTimeout=\"00:01:00\" bypassproxyOnLocal=\"false\" transactionFlow=\"false\" hostNameComparisonMode=\"StrongWildcard\" maxBufferPoolSize=\"524288\" maxReceivedMessageSize=\"65536\" messageEncoding=\"Text\" textEncoding=\"utf-8\" useDefaultWebProxy=\"true\" allowCookies=\"false\">
<readerQuotas maxDepth=\"32\" maxStringContentLength=\"8192\" maxArrayLength=\"16384\" maxBytesPerRead=\"4096\" maxNaMetableCharCount=\"16384\" />
<reliableSession ordered=\"true\" inactivityTimeout=\"00:10:00\" enabled=\"false\" />
<security mode=\"Message\">
<transport clientCredentialType=\"Windows\" proxyCredentialType=\"None\" realm=\"\" />
<message clientCredentialType=\"Windows\" negotiateServiceCredential=\"true\" algorithmSuite=\"Default\" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address=\"http://intranet.somecompany.com/InternalUSEOnly/InternalUSEOnlyService.svc\" binding=\"wsHttpBinding\" bindingConfiguration=\"WSHttpBinding_IInternalUSEOnlyService\" contract=\"InternalUSEOnlyService.IInternalUSEOnlyService\" name=\"WSHttpBinding_IInternalUSEOnlyService\">
<identity>
<dns value=\"localhost\" />
</identity>
</endpoint>
</client>
</system.serviceModel>
...
</configuration>
有人在想吗?
附加信息:在查看了以下答案之后,我尝试了两件事,但都没有成功。
最明显的变化(起初我没有注意到)是更改了客户端上的一个属性以允许cookie<system.serviceModel><bindings><wsHttpBinding><binding name=\"blah,blah,blah\" ... other properties... allowCookies=\"true\" />
。它默认为false。此外,我们的负载均衡器使用cookie来保持亲和力。但是,这并没有改变(不知道为什么)。
接下来,我尝试了客户端app.config文件中的各种安全选项。其中既包括ѭ3,又包括更详细的内容:
<security mode=\"None\">
<transport clientCredentialType=\"None\" proxyCredentialType=\"None\" />
<message clientCredentialType=\"None\" establishSecurityContext=\"false\" negotiateServiceCredential=\"false\"/>
</security>
尽管上一个设置只是我的猜测。我不对app.config进行任何服务器端更改,因为我不知道要更改什么,遗憾的是,由于我们只有1个开发Web服务器,而没有3个,因此只能进行生产测试。
解决方法
我将在这里走出去,猜测所涉及的安全性是在客户端指定的消息安全性:
<security mode=\"Message\">
<transport clientCredentialType=\"Windows\" proxyCredentialType=\"None\" realm=\"\" />
<message clientCredentialType=\"Windows\" negotiateServiceCredential=\"true\" algorithmSuite=\"Default\" />
</security>
如果要创建客户端并进行连接,则可能会缓存已协商的Windows凭据令牌。如果您未启用粘性会话,则令牌可能会传递回错误的服务器,并且将失败。我的猜测是它总是在第二次通话中?
, 这是由于使用不带粘性会话的负载平衡器引起的NTLM问题。要更正此问题,您需要配置会话亲缘关系(粘性会话)。如果不这样做,则将失败,因为一部分NTLM握手发生在一个服务器上,而另一部分发生在另一台服务器上。
, 尽管Chris和Jeff帮助我找到了答案,但实际上为我解决了这个问题的是我在Microsoft的Load Balancing Web Services上找到的这篇文章。简而言之,为解决此Web场问题,我们需要从默认的wsHttpBinding
切换到basicHttpBinding
。这并不困难,但全有还是全无。必须同时重新配置主要Web服务和每个客户端,否则它将损坏。
尽管wsHttpBinding
确实具有可以设置为true的allowCooikes属性,但显然直到建立连接后才使用它们,此时请求可能会在第一个请求上跳转服务器,从而失败。