【发布时间】:2014-01-10 22:20:07
【问题描述】:
我对这个 WCF 错误处理问题感到沮丧。我已经研究了 3 天了,似乎离解决方案还没有任何距离。
我在How to heal faulted WCF channels? 和Disposing of WCF Clients 找到了我认为的答案,但我无法让它在我的环境中工作。我正在使用 VB.NET 4.0 并尝试与供应商的服务器进行通信。他们提供了 wsdl 和 xsd 文件,这些文件 svcutil 变成了我已经合并到我的程序中的代理类。
到目前为止,我已经成功地通过了安全问题并实现了 14 个端点中的 13 个。这些似乎工作正常。实例化客户端时,不起作用的返回 Created 的 State 属性:
Dim Client = New InquireBillingInvoiceDetailsPortTypeClient(ConfigName, AppEndPoint)
If Client.State = CommunicationState.Opened Then
Client.Open()
' Code I'm not getting to
Else
Client.Abort()
Client = Nothing
End If
我知道通道可能会出现故障,但我不明白为什么在故障条件得到纠正后清除故障条件如此困难。
我也没有得到任何痕迹。与其他端点一样,此服务的客户端和服务器之间没有通信。 svclog 文件中不应该有与此实例化相关的内容吗?这可能有点矫枉过正,但我在这里也包括了 app.config 文件的相关部分。任何帮助将不胜感激。
<basicHttpBinding>
<binding name="InquireBillingInvoiceDetailsSoapHttpBinding"
closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="Certificate" />
</security>
</binding>
</basicHttpBinding>
<client>
<endpoint address="https://.../InquireBillingInvoiceDetails.jws"
behaviorConfiguration="endPointCredentialBehavior"
binding="basicHttpBinding"
bindingConfiguration="InquireBillingInvoiceDetailsSoapHttpBinding"
contract="InquireBillingInvoiceDetailsPortType"
name="InquireBillingInvoiceDetailsSoapHttpPort" />
</client>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging"
switchValue="All" >
<listeners>
<add name="xml" />
</listeners>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="U:\logs\Traces.svclog" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
【问题讨论】:
-
仅供参考……我无法确定您是在询问如何防止故障情况或如何从故障通道中恢复。
-
我无法控制服务器。我需要从故障通道状态中恢复。
-
有趣,我想知道您是否可以订阅频道事件,可能是“关闭”或“故障”,然后采取必要的操作来重置连接。
-
由于这是我第一个使用 WCF 的项目,我正在努力跟上它提供的所有功能。我不确定如何按照您的建议订阅频道事件。这很可能是所需要的,但我的研究导致人们不相信。你有什么编码建议吗?
-
我看到您从 Tim 那里得到了可能对您有用的答案。我会采取不同的方法。 WCF
ClientBase类具有内置的ChannelFactory缓存(类似于SqlConnection连接池)。通过在try/catch中进行服务调用来利用这一点,您可以在其中为每个服务调用实例化一个新的InquireBillingInvoiceDetailsPortTypeClient并在catch中执行Client.Abort()。可能看起来效率低下,但它表现良好并且是管理ClientBase继承对象的故障保险。见this article.