How to avoid common errors when consuming web services and calling page methods

1. Declare your web services using the ScriptService attribute.
If you forget to do that you will not be able to easily access the web service from the client side script. You will be missing the automatically generated client script that will allow you to call the web service from your client script code.

Example:
namespace MyNameSpace {
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService{

}
}

2.If you wish to access session state in your web service functions remember to set the EnableSession attribute to true in your web method declaration.
If you forget to do this you will not be able to access session variables inside your web service methods.

Example:

namespace MyNameSpace {
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService{
[System.Web.Services.WebMethod(EnableSession=true)]
public void myFunc() {
Session[“XVAR”] = 1;
}
}
}

More info: http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx

3. If you wish to call static page methods from client script remember to set the EnablePageMethods property of the script manager to true.
If you forget to do that you will not be able to access the static page methods from the client side script.

Example:  <ajaxToolkit:ToolkitScriptManager ID=”ScriptManager1″ runat=”server” EnablePageMethods=”true”>

4. If you wish to mix json.js client side script with the AJAX .NET client side script, use the script manager/script manager proxy Scripts property.
You might receive a client side script error.

Example:

 ajaxToolkit:ToolkitScriptManager ID=”ToolkitScriptManager1″ runat=”server”>
<Scripts>

   <asp:ScriptReference Path=”json.js” />
</Scripts>

</ajaxToolkit:ToolkitScriptManager>