HttpModule 和 WCF(AspNetCompatibilityRequirementsMode.Allowed)

发布于 2024-08-21 03:20:46 字数 959 浏览 3 评论 0原文

我在 ASP.NET 兼容模式下在 Asp.net 网页中托管 WCF 服务 (AspNetCompatibilityRequirementsMode.Allowed)。我编写了简单的 HttpModule:

public class ExceptionInterceptor : IHttpModule
{
    public ExceptionInterceptor()
    {
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(context_Error);
    }

    void context_Error(object sender, EventArgs e)
    {
        // do something
    }
}

web.config:

<httpModules>
        <add name="ExceptionInterceptor" type="HttpModules.ExceptionInterceptor, HttpModules"/>
</httpModules>

我的问题是,为什么在服务中发生未处理的异常后,代码没有进入我的模块中的 context_Error(object sender, EventArgs e) 函数。

更重要的是,代码甚至没有在Globals.asax中输入Application_Error(object sender, EventArgs e)。 有人可以向我解释一下吗?

WCF 服务中全局异常处理的最佳选择是什么?

问候

I'm hosting WCF services in Asp.net web page in ASP.NET Compatibility Mode
(AspNetCompatibilityRequirementsMode.Allowed). I've written simple HttpModule:

public class ExceptionInterceptor : IHttpModule
{
    public ExceptionInterceptor()
    {
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(context_Error);
    }

    void context_Error(object sender, EventArgs e)
    {
        // do something
    }
}

web.config:

<httpModules>
        <add name="ExceptionInterceptor" type="HttpModules.ExceptionInterceptor, HttpModules"/>
</httpModules>

My question is, why after occurence of unhandled exception in service, the code do not enter in context_Error(object sender, EventArgs e) function in my module.

What's more, the code do not even enter the Application_Error(object sender, EventArgs e) in Globals.asax.
Can someone explain that to me ?

What is the best option for global exception handling in WCF services ?

Regards

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

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

发布评论

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

评论(1

阪姬 2024-08-28 03:20:46

WCF 不是 ASP.NET - 它可能能够使用 ASP.NET 的某些基础结构,但它本身不是 ASP.NET。

为了全局处理 WCF 服务中的错误,您需要在服务上实现 IErrorHandler 接口 - 或插入为您执行此操作的 WCF 行为。

查看有关 IErrorHandler 的 MSDN 文档 -这确实是一个非常简单的界面。 HandleError 方法通常用于记录服务器上的错误以跟踪正在发生的情况,而 ProvideFault 方法用于在服务器上打开 .NET 异常进入可互操作的 SOAP 错误,以将其发送回调用客户端(可能是无法真正处理 .NET 特定异常的非 .NET 客户端)。

Rory Primrose 有一篇关于如何将 IErrorHandler 打包到 WCF 服务行为中,您可以轻松地将其添加到配置中的现有服务中 - 非常接近魔法:-) 另外,请查看另一个 有关该主题的精彩帖子,作者:Steve Barbour。

WCF is not ASP.NET - it might be able to use some of ASP.NET's infrastructure, but it's not ASP.NET per se.

In order to handle errors in a WCF service globally, you need to implement the IErrorHandler interface on your service - or plug in a WCF behavior that does this for you.

Check out the MSDN documentation on IErrorHandler - it's quite a simple interface, really. The HandleError method is typically used to log the error on the server to keep track of what's going on, while the ProvideFault method is used to turn the .NET exception on the server into an interoperable SOAP fault to send that back to the calling client (which might be a non-.NET client that can't really deal with a .NET-specific exception).

Rory Primrose has a great blog post about how to package up the IErrorHandler into a WCF service behavior which you can easily add to an existing service just in config - pretty close to magic :-) Also, check out another great post on the topic by Steve Barbour.

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