【问题标题】:HttpModule and WCF (AspNetCompatibilityRequirementsMode.Allowed)HttpModule 和 WCF (AspNetCompatibilityRequirementsMode.Allowed)
【发布时间】:2010-02-12 14:29:11
【问题描述】:

我在 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 服务中全局异常处理的最佳选择是什么?

问候

【问题讨论】:

    标签: c# .net asp.net wcf httpmodule


    【解决方案1】:

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

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

    查看MSDN documentation on IErrorHandler - 这是一个非常简单的界面,真的。 HandleError 方法通常用于在服务器上记录错误以跟踪正在发生的事情,而ProvideFault 方法用于将服务器上的 .NET 异常转换为可互操作的 SOAP 错误以将其发送回调用客户端(可能是无法真正处理特定于 .NET 的异常的非 .NET 客户端)。

    Rory Primrose 有一个 great blog post 关于如何将 IErrorHandler 打包到 WCF 服务行为中,您可以轻松地将其添加到配置中的现有服务 - 非常接近魔法 :-) 另外,请查看另一个 great post on the topic史蒂夫·巴伯(Steve Barbour)。

    【讨论】:

    • 感谢您的回复。实际上,我昨天已经从 msdn 实施了相同的解决方案。我仍然不明白为什么兼容模式不适用于 HttpModule 但实际上对我来说并非如此。
    • @Jarek:HttpModule 不起作用,因为 WCF 服务不是 ASP.NET Web 应用程序。它处理错误的方式不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多