HttpModule 和 WCF(AspNetCompatibilityRequirementsMode.Allowed)
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 theProvideFault
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.