views:

168

answers:

2

I have some ASP.NET page and webservice WebMethod() methods that I'd like to add some common code to. For example:

<WebMethod()> _
Public Function AddressLookup(ByVal zipCode As String) As Address
    #If DEBUG Then
        ' Simulate a delay
        System.Threading.Thread.Sleep(2000)
    #End If
    Return New Address()
End Function

I currently have the #If Debug code in all of my WebMethod() methods, but I am thinking there must be a better way to do this without having to actually type the code in.

Is there a way to determine if a request is to a WebMethod in Application_EndRequest so that I can add this delay project wide?

Note that some methods are Page methods and some are web service methods.

+1  A: 

You can check the request URL in Application_EndRequest to determine whether it is a web method call. E.g. something like this (sorry it's in C#):

protected void Application_EndRequest(Object sender, EventArgs e)
{
  if (Request.Url.ToString().IndexOf("MyWebService.asmx") > 0)
  {
    // Simulate a delay
    System.Threading.Thread.Sleep(2000);
  }
}
M4N
That's good... see my edit though: Some methods are PageMethods and some are from a web service, so the asmx search won't catch everything. I should have specified that from the get go.
slolife
Maybe split the page methods and web service methods into two web services? Then you can check the URL for "MyWebService.asmx". Otherwise I guess Mehrdad's approach might be better.
M4N
+1  A: 

Encapsulate the #if DEBUG code in a method and mark it with <Conditional("DEBUG")>. This way, you just write a method call in each <WebMethod>. Might be useful.

Mehrdad Afshari