在哪里捕获异常

发布于 2024-09-07 05:39:59 字数 130 浏览 7 评论 0原文

我有一个 WCF svc,分为服务层、业务逻辑层和数据访问层。

当我的 DAL 遇到异常时,我应该在那里捕获它还是让它冒泡回服务层?为什么?

请忽略此场景中的任何客户端参与,我只关心在 WCF svc 上记录异常。

I have a WCF svc separated into a Service Layer, Business Logic Layer and Data Access Layer.

When my DAL encounters an exception, should I catch it there or let it bubble back up to the Service Layer? And why?

Please disregard any client involvement for this scenario, I am only concerned with logging the exceptions on the WCF svc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惯饮孤独 2024-09-14 05:40:00

有一个术语——异常屏蔽。基本上,您应该防止系统异常进入更高级别,因为这可能会让攻击者看到您的系统架构。 WCF异常屏蔽可以捕获某些类型的异常并用其他类型的异常替换它们。例如,它可以捕获 StackOverflow 异常并将其替换为您的自定义 SystemException。如果您使用企业库,您还可以配置在替换时记录这些异常

使用Enterprise Library 3.0中的异常处理块

There is a term for that - Exception Shielding. Basically you should prevent SYSTEM exceptions to travel to higher level, because this could give an atacker a view of your system architecture. WCF Exception Shielding can catch exceptions of certain type and replace them with exceptions of other type. For example it could catch StackOverflow exception and replace it with your custom SystemException. If you use Enterprise Library you could also configure to log these exceptions when they are replaced

Using the Exception Handling Block in Enterprise Library 3.0

你曾走过我的故事 2024-09-14 05:40:00

它还取决于您如何构建解决方案。例如,如果 DAL 和 BLL 层是完全独立的组件,那么它们就无法假设谁在调用它们。因此,它们都应该捕获组件边界上的异常,记录这些异常,然后允许异常传播。他们可能希望将一般异常包装在特定于层的异常中:

catch (Exception ex)
{
    Logger.Log(ex);
    throw new DalException("Unhandled exception in DAL", ex);
}

如果您知道这些只会用作整个应用程序的一部分,那么您可以将日志记录推迟到最外层 - 该层,如果没有捕获异常,则不会记录异常。

It depends also on how you are architecting your solution. For instance, if the DAL and BLL layers are meant to be totally independent components, then they cannot make assumptions about who is calling them. As such, they should both catch exceptions on the component boundary, log those exceptions, then allow the exception to propagate. They may want to wrap the general exception in a layer-specific exception:

catch (Exception ex)
{
    Logger.Log(ex);
    throw new DalException("Unhandled exception in DAL", ex);
}

If you know these will only be used as part of your overall application, then you can defer logging to the outermost layer - the layer that, if it doesn't catch the exception, the exception won't be logged.

多彩岁月 2024-09-14 05:40:00

我想这取决于服务的使用方式以及发生异常时您想让谁(如果有的话)知道。

例如,如果您正在开发任务关键型内部业务应用程序,您可能希望异常的详细信息通过服务层传递到使用该应用程序的用户界面,以便最终用户可以快速联系开发人员解决问题。

但是,假设您的服务由公共网页使用。您可能仍然希望服务层以某种方式捕获错误,但您可能会选择将最少的信息传递给最终客户端,向最终用户提供通用错误消息,并将异常的详细信息写入错误日志中开发商审查。

最后,我仍然认为您希望异常冒泡到服务层,但是如何处理异常并决定将哪些信息传递给最终客户端取决于您。

I guess it depends on how the service is consumed and who (if anyone) you want to made aware when an exception occurs.

For example, if you're developing a mission-critical internal business application, you may want the details of the exception to bubble through the service layer to the user-interface that consumes the application so they end-user can contact a developer to quickly get the problem resolved.

However, let's say your service is consumed by a public web page. You probably still want the service layer to catch the error in some way, but you might choose to pass minimal information to the end client, provide a generic error message to the end user, and write the details of the exception to an error log for developers to review.

In the end, I still think you want the exception to bubble up to the service layer, but how you handle the exception and decide what information to pass to the end client is up to you.

掩耳倾听 2024-09-14 05:40:00

我通常使用通用错误处理程序(System.ServiceModel.Dispatcher.IErrorHandler 实现),它通过配置文件中的 ServiceBehavior 附加到 Web 服务。

IErrorHandler.ProvideFault 方法拦截从服务抛出的异常并:

  • 记录它们;

  • 按原样传递FaultExceptions;

  • 将 BLL 抛出的“业务”异常(例如违反业务规则)转换为故障代码为“发送者/客户端”的FaultExceptions;

  • 将技术异常(例如,由 DAL 抛出)转换为故障代码为“接收器/服务器”的FaultExceptions。

    将技术异常(例如,由

这样服务代码本身只包含业务代码,不需要捕获任何异常。

I usually use a generic error handler (System.ServiceModel.Dispatcher.IErrorHandler implementation) that is attached to web services by a ServiceBehavior in the configuration file.

The IErrorHandler.ProvideFault method intercepts exceptions thrown from the service and:

  • Logs them;

  • Passes FaultExceptions through as-is;

  • Converts "business" exceptions (e.g. business rule violations) thrown by the BLL to FaultExceptions with a fault code "sender/client";

  • Converts technical exceptions (e.g. thrown by the DAL) to FaultExceptions with a fault code "receiver/server".

In this way the service code itself only contains the business code and does not need to catch any exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文