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” “”>
<html xmlns=””>
<head runat=”server”>
    <title>Untitled Page</title>
    <script type=”text/javascript”>
        function printRoles() {
                    } );
    <form id=”form1″ runat=”server”>
        <asp:ScriptManager runat=”server” ID=”manager” />
        <input type=”button” value=”print roles” onclick=”printRoles()” />

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);
            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 🙂

~ by Luis Abreu on January 7, 2008.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: