views:

86

answers:

2

I have a class bMainframe that manages the connections to 4 different mainframes. It allows for the same underlying unmanaged library to be opened in specific ways and more than one mainframe to be connected to at a time. Each library has its own disposal code for the unmanaged mainframe connection resource. The wrapper also has code that calls the individual mainframe connection's disposal code.

This causes an error if someone's project does not make use of all 4 mainframes, but calls the disposal on the wrapper. (FileLoadException could not load assembly X of the 4 managed mainframes) Since that disposal code checks to see which of the 4 are not nothing/null. Even if nothing/null this is causing .net to try to load the assembly and crash.

Is the disposal code in the outer wrapper helpful or necessary? is there a way to check if the assembly for a type is even loaded that doesn't trigger.net to load the type/assembly?

I modified the code below to block the fileloadexception, but I don't believe this is the best way.

Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            ' TODO: free managed resources when explicitly called
        End If
        Try
            If Me._Linx IsNot Nothing Then
                If _Linx.cnLinx IsNot Nothing Then
                    Try
                        _Linx.Disconnect()

                    Catch ex As Exception
                        Trace.WriteLine("Error doing linx.disconnectSession")
                    End Try
                    Try
                        _Linx.Dispose()
                    Catch ex As Exception
                        Trace.WriteLine("Error doing linx.dispose")
                    End Try
                End If
            End If
        Catch ex As IO.FileLoadException
            Debug.WriteLine("Failed to load LinxFile")
        End Try
        Try
            If Me._Acaps IsNot Nothing Then
                _Acaps.Disconnect()
                _Acaps.Dispose()
            End If
        Catch ex As IO.FileLoadException
            Debug.WriteLine("Failed to load AcapsFile")
        End Try
        Try
            If Me._Dart IsNot Nothing Then
                Try
                    _Dart.Dispose()
                Catch ex As Exception
                    Trace.WriteLine("Error disposing of Dart")
                End Try
            End If
        Catch ex As IO.FileLoadException
            Debug.WriteLine("Failed to load DartFile")
        End Try
        Try
            If LpsOpen Then
                Try
                    _Lps.Dispose()
                Catch ex As Exception
                    Trace.WriteLine("Error disposing of Lps")
                End Try
            End If
        Catch ex As IO.FileLoadException
            Debug.WriteLine("Failed to load LpsFile")
        End Try

        ' TODO: free shared unmanaged resources
    End If
    Me.disposedValue = True
End Sub
A: 

check out this post which should allow you to see which assemblies are loaded

http://stackoverflow.com/questions/866487/how-to-retrieve-info-on-a-loaded-assembly-at-runtime-c-net

Aaron M
A: 

Perhaps this could be handled with four separate classes inheriting from a base class that would have slightly different implementation in their dispose functions... then you can iterate through an array and dispose them all if you are using more than one in some case. Something doesn't seem right about having to figure out what resources are included at run-time in this particular use since you know the different mainframes at design time.

Also, after you've released objects for the garbage collector to pick up, it may be a good idea to run GC.Collect() so the garbage collector runs immediately.

Jrud