简体   繁体   English

在COM +(VB)中处理自定义.Net异常?

[英]Handling custom .Net exceptions in COM+ (VB)?

I'm about to start writing a .Net component which will be called from a VB COM+ service (the new component is a DLL which calls out to a webservice and returns based on the response). 我将要开始编写一个.Net组件,该组件将从VB COM +服务中调用(新组件是一个DLL,它可以调用Web服务并根据响应返回)。 I'm not sure how to handle error conditions that might occur in the .Net code in the calling VB. 我不确定如何处理在调用VB的.Net代码中可能发生的错误情况。

The two types of errors I'm worried about are: 我担心的两种错误是:

  • exceptions that I might like to throw if a precondition is not met (eg the start date supplied as a parameter is less than 3 months after the end date supplied as a parameter; I might want to throw a StartDateNotValidException) 如果不满足前提条件,我可能想抛出的异常(例如,作为参数提供的开始日期比作为参数提供的结束日期还不到3个月;我可能想抛出StartDateNotValidException)
  • exceptions that are likely to happen as part of the webservice call (time out, 404 etc). 可能会在网络服务调用中发生的异常(超时,404等)。

I'd like to return some specific feedback to the user and/or log some information to a log if either of these occur. 我想向用户返回一些特定的反馈和/或将某些信息记录到日志(如果其中任何一种发生)。 I'd thought about returning Int return codes from the .Net code and avoiding Exceptions, but it's possible that the calling VB code may eventually be migrated to .Net, so I'd like to use Exceptions if possible. 我曾考虑过从.Net代码返回Int返回代码并避免使用Exceptions,但是有可能最终将调用的VB代码移植到.Net,因此,如果可能的话,我想使用Exceptions。

I read on MSDN that COM Interop will automatically convert standard library exceptions to HRESULTs; 我在MSDN上读到,COM Interop将自动将标准库异常转换为HRESULT。 has anyone had any experience using this? 有没有人有使用过的经验? Could this be used to translate custom .Net exceptions to error conditions that I can handle in VB? 可以用来将自定义.Net异常转换为我可以在VB中处理的错误条件吗?

Thanks, Nick 谢谢,尼克

I am sure I have seen it done before, but it was in my previous job so I can't check the details. 我确定我以前看过它,但是那是我以前的工作,所以我无法检查细节。 I think we inherited our exception classes from COMException and set the correct ErrorCode (which should be translated as HResult in unmanaged code). 我认为我们从COMException继承了异常类,并设置了正确的ErrorCode(应在非托管代码中将其翻译为HResult)。 This is quite nice, because you can use HResult in unmanaged code as well as properly handle typed exception in managed client. 这非常好,因为您可以在非托管代码中使用HResult,也可以在托管客户端中正确处理类型化的异常。

Please let me know if it actually works. 请让我知道它是否确实有效。

I ended up using the following method: 我最终使用以下方法:

  • have a big Dictionary that maps our application-specific VB error codes to our application-specific C# custom exceptions 有一个很大的字典,可将我们特定于应用程序的VB错误代码映射到特定于应用程序的C#自定义异常
  • write a method that converts C# exceptions to VB error codes (and vice-versa) 编写将C#异常转换为VB错误代码的方法(反之亦然)
  • return an array of strings from the methods containing any business exceptions that occurred in the method call (there are only currently two of these) 从方法返回一个字符串数组,其中包含方法调用中发生的任何业务异常(目前只有两个)

The reason for doing this is that it was going to be too cumbersome to adapt our exception tree to extend COMException (more cumbersome than the above anyways). 这样做的原因是,要适应我们的异常树来扩展COMException太麻烦了(总之比上面的麻烦)。 So, I never tried out inheriting from COMException and checking whether the ErrorCode got correctly converted to an HResult. 因此,我从未尝试过从COMException继承并检查ErrorCode是否正确转换为HResult。

Thanks for the suggestion though, and apologies for not trying it out. 不过,感谢您的建议,也很抱歉没有尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM