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.
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
Output