且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WCF服务不可用-断电-什么错误

更新时间:2022-03-18 23:17:26

应在发起服务调用的客户端处理异常.这是讨论过的类似内容:
http://***.com/questions/2816901/handling-service-unavailable- errors-in-asp-net [ ^ ]
The exception should be handled at the client side from where the service call is initiated. Here is something similar discussed:
http://***.com/questions/2816901/handling-service-unavailable-errors-in-asp-net[^]


谢谢.在阅读和研究之后,我决定摆脱对象数据源,并以编程方式绑定listview.该服务返回一个列表,因此我正在检查列表计数以及它是否> 0我将数据源设置为列表,然后将其绑定.如果服务关闭,我将隐藏列表视图.如果服务关闭,错误将被捕获在代码的第一部分.我仍然感到惊讶的是,当服务关闭时整个页面都炸毁了,并且没有设置可以处理的设置,但这很容易.这是代码(这是背后代码中的唯一代码):

< pre lang ="vb">受保护的子lvCurrentTopics_Load(作为对象发送,作为System.EventArgs发送)处理lvCurrentTopics.Load
试试

''服务返回对象列表
作为新Generic.List(srbcweb_wcf.CurrentTopic)的变暗

''打开客户端并填充列表
使用c作为新的srbcweb_wcf.SRBCWeb_wcfClient

c.Open()

lst = c.GetCurrentTopics

c.Close()

最终使用

''检查列表是否已填充并设置listview的数据源
如果不是lst.Count = 0,则

lvCurrentTopics.DataSource = lst

lvCurrentTopics.DataBind()

其他

lvCurrentTopics.Visible = False

如果结束

异常捕获
lvCurrentTopics.Visible = False
''给我发错误
结束尝试

结束Sub</pre>
Thanks for the idea. After reading and researching, I decided to get rid of the object datasource and bind the listview programatically. The service returns a list so I''m checking the list count and if it''s > 0 I''m setting the datasource to the list and then binding it. If the service is down, I hide the listview. If the service is down the error will be caught in the first section of the code. I''m still surprised the whole page blew up when the service was down and there isn''t a setting to handle it, but this was easy enough. Here is the code (it''s the only code in the code-behind):

<pre lang="vb"> Protected Sub lvCurrentTopics_Load(sender As Object, e As System.EventArgs) Handles lvCurrentTopics.Load
Try

'' the service returns a list of objects
Dim lst As New Generic.List(Of srbcweb_wcf.CurrentTopic)

'' open the client and populated the list
Using c As New srbcweb_wcf.SRBCWeb_wcfClient

c.Open()

lst = c.GetCurrentTopics

c.Close()

End Using

'' check if the list is populated and set the datasource of the listview
If Not lst.Count = 0 Then

lvCurrentTopics.DataSource = lst

lvCurrentTopics.DataBind()

Else

lvCurrentTopics.Visible = False

End If

Catch ex As Exception
lvCurrentTopics.Visible = False
'' send me the error
End Try

End Sub</pre>