LA.NET [EN]

Jan 07

ASP.NET AJAX: generating the proxy''s JS code on the fly

Posted in ASP.NET MS AJAX      Comments Off on ASP.NET AJAX: generating the proxy''s JS code on the fly

Before talking about the ProxyGenerator class, I”ll have to add one more observations on the previous posts on the new role service: for the code I”ve shown to work you do need to have an authenticated user. The easiest way to see the previous code working is just to create a new login page which uses the FormsAuthentication class to authenticate the user and then redirect to the page that contained the code presented on the previous posts.

Now, lets go on and see how to generate the JS code for a web service proxy on the fly. If you fire up Reflector and navigate to the System.Web.Script.Services namespace you”ll find  a class called ProxyGenerator. The class exposes a single method: GetClientProxyType. It expects to receive a type (the type of the service you”re building a proxy to access), a path (to the service file) and a boolean (which lets you specify if you want it to generate debug or release code – true means debug code). Lets build a simple page that reuses the custom role WCF service (even though it was build to be used with the Role client service, nothing prevents you from injecting it on a page and calling its methods through the generated client proxy):

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default2.aspx.cs” Inherits=”Default2″ %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
    <title>Untitled Page</title>
    <script type=”text/javascript”>
        function printRoles() {
            la.net.tests.CustomRoleService.GetRolesForCurrentUser(
                    function(res){
                        alert(res);
                    } );
        }
    </script>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:ScriptManager runat=”server” ID=”manager” />
        <input type=”button” value=”print roles” onclick=”printRoles()” />
    </div>
    </form>
</body>
</html>

As you can see, we”re not adding a web service reference to the ScriptManager control. In this case, we”re injecting the proxy by using the ProxyGenerator class, as you can see in the following snippet:

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String serviceJS = ProxyGenerator.GetClientProxyScript(
            typeof (CustomRoleService), “CustomRoleService.svc”, false);
        ClientScript.RegisterStartupScript(
            typeof(Default2), “service”,serviceJS,true);
    }
}

As you can see, we”re using an instance of the ClientScriptManager to insert the JS proxy on the page (do notice that if you need to insert JS code during partial postbacks, you should use the static RegisterXXX methods available on the ScriptManager class). If you”re curious about the difference in the JS code generated when you toggle the debug parameter, then just try it and check the source of the page :)