Benno
Benno

Reputation: 2534

Is wcf web service.close needed

I have some code shown below, which appears to work as needed. My question is - is it necessary to have a webservice.close? This will go out of scope at the end of the sub. The main reason I ask is the wsClient in finally gets a code warning,

Variable 'wsClient' is used before it has been assigned a value. A null reference exception could result at runtime.

The best outcome would probably be to restructure and keep the .close and get rid of the warning.

    Dim wsClient As wsGates.ConsoleClient

    Try
        wsClient = New wsGates.ConsoleClient
        Dim wsRet As wsGates.clsReturn

        wsRet = wsClient.Phase1Validate("15024")
    Catch

    Finally
        If wsClient IsNot Nothing Then wsClient.Close()
    End Try

End Sub

Upvotes: 0

Views: 48

Answers (2)

Dark Falcon
Dark Falcon

Reputation: 44181

If you do not close it, the underlying channel may remain open. While this may eventually be closed when the object is GC'd or your application exits, it is best to close it to release resources as soon as possible.

Rather than using a Try statement, I would recommend a Using:

Using wsClient As New wsGates.ConsoleClient
    Dim wsRet As wsGates.clsReturn

    wsRet = wsClient.Phase1Validate("15024")
End Using

Upvotes: 1

Raju Padhara
Raju Padhara

Reputation: 707

not required to close wsClient because its life only on that method only. After that method execute it auto manically destroy object.

Upvotes: 0

Related Questions