ASMX equivalent of Page_Init?
Asked Answered
L

3

11

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!

Lager answered 29/6, 2010 at 20:56 Comment(0)
A
10

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;
        }
    }
Alphosis answered 29/6, 2010 at 21:19 Comment(2)
+1 - good point. Forgot that web service calls are "stateless", so the constructor will be called for each web method request. Nice one.Obturate
Be careful, all request will execute the WebService1() constructor (It is not happend just one time).Quickie
O
4

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.

Obturate answered 29/6, 2010 at 21:14 Comment(1)
Beter route than the accepted anser imho. (especially if you need to do something with the request context.)Germin
F
0

They do not have similar 'life cycles'

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

Footplate answered 29/6, 2010 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.