为 Visual Studio 2010 VsPackage 创建全局异常处理程序
在 Visual Studio 的早期版本中,扩展(更具体地说是插件)有一个入口点,即 OnConnect
方法。从那里可以执行所有 Visual Studio 操作,例如添加菜单项和设置全局异常处理程序,以优雅地处理未处理的异常。
在 Visual Studio 2010 的 VsPackage 中,事情的工作方式有所不同 - 应用程序的许多部分(例如 < code>IWpfTextView)由 MEF 组成,并且在没有包的显式控制的情况下加载,使得处理来自这些异常的异常非常困难。
如果没有将所有此类组件显式包装在它们自己的 try..catch
中,我如何在 VsPackage 中有效地实现(非关键)异常的全局处理程序? 同样,我们的目标是拥有类似 ReSharper 的异常对话框的功能,它可以在不导致 Visual Studio 崩溃的情况下处理全局异常。
In previous versions of Visual Studio, there was a single entry point to an extension (more specifically, an addin), the OnConnect
method. From there it was possible to perform all the Visual Studio operations, such as adding menu items, and setting up a global exception handler, to gracefully handle unhandled exceptions.
In Visual Studio 2010's VsPackage, things work differently - many parts of the application (such as IWpfTextView
) are composed by MEF and loaded without the explicit control of the package, making handling any exceptions coming from those very difficult.
Without explicitly wrapping all such components in a try..catch
of their own, how can I efficiently implement a global handler for (non-critical) exceptions in my VsPackage?
Again, the goal is having something like ReSharper's exception dialog, where it can handle global exceptions without crashing the Visual Studio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过阅读官方 VsExtensibility 论坛并根据 这个 和 此 线程有没有这样一个地方可以挂钩来捕获所有异常。因此,典型的解决方案是在任何地方使用 try...catches 。
我建议使用 AOP 框架之一,例如 PostSharp 来通过属性来完成此操作,以消除大量蹩脚的重复代码,其唯一目的是处理未处理的异常。
I have investigated this topic by reading official VsExtensibility forum and according to this and this threads there is no such one place to hook for catching all exceptions. So typical solution will be to use try... catches everywhere.
I suggest to use one of the AOP frameworks like PostSharp to accomplish this with attributes in order to eliminate tons of crappy duplicated code that's only purpose is to handle unhandled exceptions.