2
votes

I have added some web methods to my ASP.NET page like the following to enable AJAX calls using jQuery on the client side of my application:

public partial class MyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Page load logic...
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static string MyWebMethod()
    {
        // web method logic...
    }
}

However, I noticed using the debugger that my web methods are being processed on a different AppDomain than my ASP.NET Page's AppDomain, hence each one of them has its own set of static variables which is causing me some trouble.

So I would like to know if it's somehow possible to host WebMethods (or a web-service) and an ASP.NET app on the same AppDomain, enabling both of them access to the same set of static variables?

Thanks in advance!

EDIT 1

I'm hosting my app on IIS 7.0

EDIT 2

As Jupaol's answer clearly shows it is possible to host WebMethods and an ASP.NET page on the same appDomain, nevertheless here is what I found after running more tests:

If my jQuery.ajax call sets its URL like this:

$.ajax({
         url: "/MyFolder/MyPage.aspx/MyWebMethod",
         contentType: "application/json; charset=utf-8",
         success: AjaxSuccess,
         error: AjaxError
});

IIS creates two appDomains on my Web site (same web site key), one for the page request and another for the WebMethod request. Their FriendlyNames are showed below:

  • "/LM/W3SVC/1/ROOT-6-129872125368848248" (WebMethod)
  • "/LM/W3SVC/1/ROOT/WebApp-5-129872125273487796" (ASP.NET page request)

However, if the jQuery.ajax call URL is set like this:

$.ajax({
         url: "MyPage.aspx/MyWebMethod",
         contentType: "application/json; charset=utf-8",
         success: AjaxSuccess,
         error: AjaxError
});

Only one appDomain gets created to serve both requests:

  • "/LM/W3SVC/1/ROOT/WebApp-7-129872126989518526"

Unfortunately I wasn't able to find an explanation for this behaviour, and the only thing I can think of that may be related to this issue is that my web site uses https on some pages and not in others.

1
How did you realized that? As far as I know, that's not possible, they could be being executed on different threads but of the same AppDomainJupaol
I checked the AppDomain.CurrentDomain property both when Page_Load and MyWebMethod were executed and they didn't match.Thomas C. G. de Vilhena
I've never seen this in the debugger, how can you see this? Is the web service in your project/solution? And why in the world would a web service need access to static variables of the calling application? A web service is supposed to be a black box, you pass any data to it, as long as it conforms to the WSDL, and it does what it does, needing nothing more than that, according to the WSDL. None of the applications accessing it need know how it works. This is a really unusual question. It should never need access to the static variables of the app consuming it, it is standalone.GrayFox374
continued... Are you declaring AppDomians in your code? If so, why? Your posted code is a web method. A web service is something similar, but very different, too. Which is it? The title of this may be misleading. And what is the reason for making the web method static? There are lots of questions here.GrayFox374
@GrayFox374 It's not a web service, they are called Page Methods which behave like Web ServicesJupaol

1 Answers

3
votes

Are you sure that you are comparing the AppDomain's ID???

I just made an experiment and they do belong to the same AppDomain

That could not always be true if you are explicitly creating AppDomains in code.

Example: (I just updated an old example I had, focus on the AppDomain.CurrentDomain.Id)

Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {
        this.lblMessage.Text += "AppDomain ID: " + AppDomain.CurrentDomain.Id.ToString() + "<br/>";
    }

    [WebMethod]
    public static string Execute1()
    {
        JavaScriptSerializer j = new JavaScriptSerializer();
        string r = string.Empty;
        var o = Observable.Start(() =>
        {
            Thread.Sleep(2000);
            r = "My Name1: " + DateTime.Now.ToString() + " Background thread: " + Thread.CurrentThread.ManagedThreadId.ToString();
        }, Scheduler.NewThread);
        o.First();
        r += " Main thread: " + Thread.CurrentThread.ManagedThreadId.ToString();
        r += " AppDomain ID: " + AppDomain.CurrentDomain.Id.ToString();
        r = j.Serialize(new { res = r });
        return r;
    }

Output

enter image description here