如何中止基于逻辑在构造一个ASMX请求? [英] How to abort an ASMX request based on logic in the constructor?

查看:161
本文介绍了如何中止基于逻辑在构造一个ASMX请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共同的基类,所有我的web服务ASMX类将继承。在构造函数中,我想要做一些常见的认证检查;如果他们失败了,我想停止加工马上(子类的code将不会被执行),并返回401状态 - code响应给调用者。

I have a common base class from which all my ASMX webservice classes will inherit. In the constructor, I want to do some common authentication checks; if they fail, I would like to halt processing right away (subclass's code would not get executed) and return a 401-status-code response to the caller.

不过,似乎这样做的共同ASPX样的方式并不工作:​​

However, the common ASPX-like ways of doing this don't seem to work:


  1. Context.Response.End();总是踢回ThreadAborted例外主叫,500岗位code响应中。即使我明确地设置Context.Response.Status code = 401主叫端()之前,它被忽略。其结果仍然是一个500响应,消息总是线程中止的异常。

  2. MSDN 的建议我使用的HttpContext。 Current.ApplicationInstance.CompleteRequest()代替。但是,这并不能阻止下游加工:我的子类的功能,如果构造做了什么仍然执行。 (有点失败在构造函数中检查授权的目的。)

  3. 我可以抛出一个新的HttpException。这是因为它确实prevent下游加工好一点,至少它给了我控制了异常信息返回给调用者。然而,它不健全,该反应仍然总是500。

  4. 我可以定义一个DoProcessing实例var和在构造函数中把它设置为真/假。然后在每个子类中的每一个的WebMethod一个if(DoProcessing)块内包装它的功能......但让我们面对它,这是可怕的!

  1. Context.Response.End(); always kicks back a ThreadAborted exception to the caller, within a 500-status-code response. Even if I explicitly set Context.Response.StatusCode = 401 before calling End(), it is ignored. The result is still a 500-response, and the message is always "thread-aborted-exception".
  2. MSDN suggests I use HttpContext.Current.ApplicationInstance.CompleteRequest() instead. However, this does not stop downstream processing: my subclass's functions are still executed as if the constructor had done nothing. (Kind of defeats the purpose of checking authorization in the constructor.)
  3. I can throw a new HttpException. This is a little better in that it does prevent downstream processing, and at least it gives me control over the exception Message returned to the caller. However, it isn't perfect in that the response is still always a 500.
  4. I can define a DoProcessing instance var, and set it to true/false within the constructor. Then have every single WebMethod in every single subclass wrap its functionality within an if (DoProcessing) block... but let's face it, that's hideous!

有没有实现这种功能更好/更彻底的方式,所以是很常见的我所有的ASMX类?

Is there a better / more thorough way to implement this sort of functionality, so it is common to all my ASMX classes?

修改:接受约翰的回答,因为它可能是最好的办法。但是,由于客户不愿采取额外的第三方code和一定程度上与AOP FUD,我们没有采取这种做法。我们结束了与上面的选项#3下去,因为它似乎罢工的速度实现性和灵活性之间的最佳平衡,仍然满足要求。

edit: Accepting John's answer, as it is probably the best approach. However, due to client reluctance to adopt additional 3rd-party code, and some degree of FUD with AOP, we didn't take that approach. We ended up going with option #3 above, as it seemed to strike the best balance between speed-of-implementation and flexibility, and still fulfill the requirements.

推荐答案

这样做会切换到WCF,这对这种情况下的明确支持的最好方法。

The best way to do it would be to switch to WCF, which has explicit support for such a scenario.

如果您仍然必须使用ASMX,那么你最好的选择是调用每个Web方法的基类的方法。你可能想使用类似PostSharp为神奇地使所有的你的web方法来调用基类的方法。

If you must still use ASMX, then your best bet is to call the base class methods from each web method. You might want to use something like PostSharp to "magically" cause all of your web methods to call the base class method.

这篇关于如何中止基于逻辑在构造一个ASMX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆