service.close()与service.abort()-WCF示例 [英] service.close() vs. service.abort() - WCF example

查看:135
本文介绍了service.close()与service.abort()-WCF示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其中一个WCF教程中,我看到了以下示例代码:

In one of the WCF tutorials, I saw the following sample code:

Dim service as ...(a WCF service )

try

   ..

   service.close()

catch ex as Exception()
  ... 

   service.abort()

end try

这是确保即使在错误情况下也可以释放资源(即连接)的正确方法吗?

Is this the correct way to ensure that resources (i.e. connections) are released even under error conditions?

推荐答案

我很幸运使用此模型:

Dim service As New MyService()
Dim closed As Boolean = False
Try
    service.Open()
    If Not service.State = ServiceModel.CommunicationState.Opened Then
        ''Handle a not-opened state here
    End If
    service.MyMethod()
    service.Close()
    closed = true
Catch ex As Exception
    ''Handle errors here
Finally
    If Not closed Then
        service.Abort()
    End If
End Try
service = Nothing

这篇关于service.close()与service.abort()-WCF示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆