ASMX 相当于 Page_Init?

发布于 2024-09-07 12:45:56 字数 441 浏览 2 评论 0原文

我有一些代码想在调用 ASMX 函数的生命周期的早期执行。对于我们的 ASPX 页面,此代码位于基类的 Page_Init() 函数中,我们所有的 ASPX 页面都继承自该基类。

ASMX 是否有相当于 ASPX 的 Page_Init() 函数?

更好的是,是否有像 ASPX 那样的 ASMX 生命周期图? http://msdn.microsoft.com/en-us/library/ms178472.aspx

如果存在与 Page_Init() 等效的 ASMX,我假设我可以在公共基类中实现代码,我的所有 ASMX 类都可以继承该基类,对吗?

编辑: 很好的回复 - 感谢您的帮助!

I have some code I would like to execute very early in the lifecycle of a call to an ASMX function. For our ASPX pages, this code is in the Page_Init() function on a base class, from which all our ASPX pages inherit.

Is there an ASMX equivalent to the ASPX's Page_Init() function?

Better yet, is there an ASMX lifecycle diagram like the ASPX one? http://msdn.microsoft.com/en-us/library/ms178472.aspx

If there is an ASMX equivalent to Page_Init(), I assume I can implement code in a common base class, from which all my ASMX classes can inherit, correct?

EDIT:
Great responses - thanks for your help!

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

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

发布评论

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

评论(3

弱骨蛰伏 2024-09-14 12:46:59

它们没有类似的“生命周期”。

唯一的两个“事件”是请求和响应。

They do not have similar 'life cycles'

The only 2 'events' are the Request and the Response.

绾颜 2024-09-14 12:46:47

非常好的问题!

不完全确定,但我相信 ASMX Web 服务的执行与 ASPX 页面略有不同 - 没有“页面生命周期”(即没有初始化控件来呈现 HTML - 因为响应通常是 XML)。

您唯一的选择是挂钩 Global.asax 中的应用程序事件之一 - 唯一合适的事件是 Application_PreRequestHandlerExecute

您可以尝试 Application_BeginRequest,但我相信这仅适用于 ASP.NET 页面请求,不适用于 Web 服务调用。

您的另一个选择(正如您所说)是为您的 Web 服务创建一个基类,然后在第一行调用所有 Web 方法中的通用基方法。您必须在所有 Web 方法中重复此调用。或者,如果您将所有 Web 方法都放在一个 Web 服务文件 (ASMX) 中,则只需创建一个常规静态方法(不要使用 WebMethod 属性装饰它)并调用它。

Very good question!

Not entirely sure, but i believe that execution of ASMX Web Services is slightly different to ASPX Pages - there is no "Page Lifecycle" (i.e there is no initialization of controls in order to render HTML - as the response is generally XML).

Your only options would be to hook into one of the Application events in Global.asax - the only suitable event would be Application_PreRequestHandlerExecute.

You can try Application_BeginRequest, but i believe this is only for ASP.NET Page Requests, not Web Service calls.

You're other option (as you said) is to create a base class for your web services, then call the common base method in all of your web methods at the very first line. You would have to repeat this call in ALL of your web methods. Or if you have all your web methods in a single web service file (ASMX), then just create a regular static method (dont decorate it with the WebMethod attribute) and call that.

浅笑轻吟梦一曲 2024-09-14 12:46:39

在 asmx Web 服务中实际上并没有这样的东西,System.Web.Services.WebService 没有事件。最好的选择是创建一个默认构造函数并将其放入其中。

例如

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {
        private string strRetVal;

        public WebService1()
        {
            strRetVal = "Hello World";
        }

        [WebMethod]
        public string HelloWorld()
        {
            return strRetVal;
        }
    }

There isn't really such a thing in an asmx web service, System.Web.Services.WebService has no events. Your best bet is to create a default constructor and put it in there.

e.g.

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {
        private string strRetVal;

        public WebService1()
        {
            strRetVal = "Hello World";
        }

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