Denis
Denis

Reputation: 12077

Strange 'Dispose' Error when calling with 'Using' or directly calling 'DIspose'

I have this code which works:

Option Strict On
Imports System
Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Description
...
 Private Property Channel As IWSOServiceContract

 Public Sub SomeMethod(ByVal url As String)
    Using ChlFactory As ChannelFactory(Of IWSOServiceContract) = New ChannelFactory(Of IWSOServiceContract)(New WebHttpBinding(), url)
       ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
       Channel = ChlFactory.CreateChannel()
    End Using
 End Sub
...

But then when I refactor it to:

Option Strict On
Imports System
Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Description
...
Private Property ChlFactory As ChannelFactory(Of IWSOServiceContract)
Private Property Channel As IWSOServiceContract

Public Sub SomeMethod(ByVal url As String)
    ChlFactory = New ChannelFactory(Of IWSOServiceContract)(New WebHttpBinding(), url)
    ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
    Channel = ChlFactory.CreateChannel()
    ChlFactory.Dispose()   '<-- ERROR HERE BUT HOW DID USING WORK BEFORE?
End Sub 
...

Am completely at a loss without any explanation of why there is an error "Dispose is not a member of ChannelFactory" in the second method but not in the first method?

Upvotes: 1

Views: 62

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

That's because ChannelFactory implements IDisposable.Dispose explicitly. You'd have to cast it to IDisposable to call Dispose.

Using statement is smart enough to do the casting for you.

Upvotes: 3

Related Questions