This post covers second part of my investigations in how we can execute some class library method remotely from javascript without using WebService call or PageMethod call. First part of the article is available on this link.
Saying shortly I created assembly that needs minimum configuration to redirect call to any class library referenced in the web site !
I placed source code on my website - so you can download it and see how I did it, the code is too large to describe it in this post. But saying shortly I used ASP.NET AJAX framework source code, and especially the part of it where AJAX framework is using AppService.axd path to access Profile and Membership web services.
Ok lets see how you can include this functionality to your web site.
Sample solution looks like this:
Here you can see three Projects:
1) AjaxServiceLoader is the assembly that allows you to call http handlers the same way you call web services (almost the same).
2) WizardFramework - is some website on which I need to call methods from some another assembly right from the browser.
3) MyCustomAssembly - is the assembly I want to call methods in, from my web site.
MyCustom assembly contains only one simple class, the listing shows it's contents:
using System.Web.Services;
usingSystem.Web.Script.Services;
namespaceMyCustomAssembly
{
[ScriptService]
public classSomeClass
{
[WebMethod]
public objectMyMethod1(int someInteger, int anotherInteger)
{
returnsomeInteger + anotherInteger;
}
[WebMethod]
public objectMyMethod2(stringsomeString, stringanotherString)
{
returnsomeString + " "+ anotherString;
}
}
}
Ok thats all about MyCustomAssembly, now the most interesting part, how I configure WizardFramework website to use that assembly.
First to use methods inside assembly we need reference MyCustomAssembly in the website. AjaxServiceLoader should be also referenced, because we will use it to achieve what we need.
Web.config file should contain the following sections:
<httpHandlers> ... <add verb="*" path="*_LoaderService.axd" validate="false" type="AjaxServiceLoader.LoaderServiceHandler, AjaxServiceLoader"/> ... </httpHandlers>
This is needed configuration entry - it says to AjaxServiceLoader assembly that all requests ending with _LoaderService.axd should be served as script service calls.
And the second configuration where we actually define where to search for Type and method we want to execute:
first register section as follows:
<section name="ServiceTypeDefinitions"
type="AjaxServiceLoader.ServiceTypeDefinitionsSection, AjaxServiceLoader"allowDefinition="Everywhere"allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />
And second just list which calls will be redirected to which types:
<ServiceTypeDefinitions> <serviceTypes> <clear /> <add name="SomeName" serviceKey="SomeKey" serviceType="MyCustomAssembly.SomeClass, MyCustomAssembly" /> </serviceTypes> </ServiceTypeDefinitions>
Here serviceType attribute points to type that will be executed for request SomeKey_LoaderService.axd .
That's it, we finished configuration - you can see how easy it is, you can list there multiply types with multiply keys - even from different assemblies.
Last thing is how we use the service, so there is a listing for Default.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <asp:ScriptReference Path="~/SomeKey_LoaderService.axd/js" /> </Scripts> </asp:ScriptManager> <script type="text/javascript"> function GetMethod1() { MyCustomAssembly.SomeClass.MyMethod1( 2, 3, GotMethod1, null, null ); } // result should be 5 function GotMethod1(result) { $get("resultsDiv").innerHTML = result; } function GetMethod2() { MyCustomAssembly.SomeClass.MyMethod2( "Hello", "world", GotMethod2, null, null ); } // result should be "Hello world function GotMethod2(result) { $get("resultsDiv").innerHTML = result; } </script> <div> <input type="button" value="Get Method1 from SomeClass" onclick="GetMethod1();return false;" /> <input type="button" value="Get Method2 from SomeClass" onclick="GetMethod2();return false;" /> </div> <div id="resultsDiv"> </div> </form> </body> </html>
Also very simple, the script reference we add to ScriptManager control:
<Scripts> <asp:ScriptReference Path="~/SomeKey_LoaderService.axd/js" /> </Scripts>
does all for us, The corresponding Handler is loaded, it reads the information from web.config file and finds that SomeKey key is attached to the SomeClass class in the MyCustomAssembly assembly. Then it uses reflection to reflect methods, and their expected parameters with types, and generates a client proxy script that allows us to use the type right from javascript.
As you see I use MyCustomAssembly.SomeClass.MyMethod1 and pass some integer parameters that will be passed to actual method in that assembly.
I hope you like the simplicity and will use the code as you wish. If you like the idea please provide some feedback.
Hope this helps.

0 comments:
Post a Comment