我可以处置这些非托管资源而不需要引用每个资源吗?
我有一个 bMainframe 类,它管理与 4 个不同大型机的连接。 它允许以特定方式打开相同的底层非托管库,并且一次可以连接到多个主机。 每个库都有自己的非托管大型机连接资源的处置代码。 包装器还具有调用各个大型机连接的处理代码的代码。
如果某人的项目没有使用所有 4 个大型机,而是调用包装器上的处理,这会导致错误。 (FileLoadException 无法加载 4 个托管主机的程序集 X)因为该处理代码会检查 4 个主机中的哪一个不是 Nothing/null。 即使没有/null,这也会导致 .net 尝试加载程序集并崩溃。
外包装中的处置代码是否有用或必要? 有没有办法检查类型的程序集是否已加载,而不会触发.net 加载类型/程序集?
我修改了下面的代码来阻止 fileloadexception,但我不认为这是最好的方法。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看这篇文章,它应该允许您查看加载了哪些程序集
如何在运行时检索已加载程序集的信息? (c#、.NET)
check out this post which should allow you to see which assemblies are loaded
How to retrieve info on a loaded assembly at runtime? (c# , .NET)
也许这可以通过从基类继承的四个单独的类来处理,这些类在其处置函数中的实现略有不同......然后,如果您在某些情况下使用多个数组,则可以迭代一个数组并处置它们全部。 由于您在设计时就知道不同的大型机,因此必须弄清楚在这种特定用途中运行时包含哪些资源似乎不太正确。
另外,在释放对象供垃圾收集器拾取后,运行 GC.Collect() 可能是一个好主意,以便垃圾收集器立即运行。
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.