Click here to Skip to main content
13,865,507 members
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 30 May 2007
Licenced CPOL

Assembly-based webservice and Javascript proxy using reflection

, 30 May 2007
Rate this:
Please Sign up or sign in to vote.
ASP.NET and Ajax Webservices, not from a .asmx file, but from a compiled assembly with a little bit of reflection


Anyone who has been building .NET application frameworks, custom controls or just likes to separate his concerns, will have come across the same problem I was having. I hope I can help some of you without having to go to the lengths I did, but let me start with explaining the problem. It's a pretty long story so hold on; the level in this article ranges from beginner to advanced in a lot of different areas.

The problem

When you want to develop Custom Controls with Javascript call-back functionality, you can use the ICallBack and IScriptable interface. These interfaces implement a call from your class and a return call to your class by use of Javascript, without having to implement an update panel or page refresh. The ICallback interface has a problem, though: it has no nice JSON (De)Serialization. It's confined to one function per component and although implementing more is possible, it is not very manageable.

How it should be

If you have ever worked with Webservices in ASP.NET, you will have seen the power of this mechanism. This is especially the case when in combination with ASP.NET Ajax 1.0, which generates a Javascript proxy for you. For those of you who haven't used ASP.NET Webservices, here's a small recap: You create an ASMX file in your project, fill it with a class containing a few attributes meant for Webservices and you are done! No SOAP creation, no JSON (DE)Serialization, no proxy writing. The only thing you still have to do is to register it with your <ScriptManager> for it to generate the Javascript Proxy. Would this not be great to have in our custom control library?

Yet another problem

The big problem with this approach is that this functionality only works with not-yet-compiled ASMX files in the web directory. You have no "out of the box" way of pointing this functionality to a compiled assembly. To take on this problem, we first take a look at how the current functionality works.

How it works by default

The ASMX files are translated into a web service and Javascript proxy through an HTTPHandler. This HTTPHandler is set in the config.xml file.

    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" 


The handler you see here is the one that is created by default in a new "ASP.NET Ajax-enabled Web Application." When a request is done for a file with the extension ASMX ("*.asmx"), it will forward that Request to the ScriptHandlerFactory. The scripthandler factory then creates the web service, Javascript Proxy or Javascript Method handler for you. When you add the web service to the ScriptManager, it will generate a <script src="Webserver.asmx/js"/> on your webpage. The problem with this class is that it will check for the ASMX file and nothing else. The class has a bigger problem, in that almost everything is Internal and Private. If you don't know what that means, it means that extending or changing anything is impossible.

Reflect it

Okay, maybe it's not impossible. Everything compiles into managed code and, with reflection, we can get into every little corner we like. Even private constructors can be reached through one of the most powerful components of the .NET Framework. There are a lot of tutorials out there about Reflection, so in this article I'm going to assume you know about the basics.

Beginning the solution

We start out with a new empty "ASP.NET AJAX-enabled Web application," which comes with ASP.NET Ajax 1.0. Then we add a second empty C# class library project as a second project and we reference the second project in the first.


To implement our own code, we are going to create our own HttpHandlerFactory in our CustomControl Library. We name it AutoDiscoveryWebServiceHandlerFactory with the interface IHttpHandlerFactory, which implements :

public IHttpHandler GetHandler(HttpContext context, 
    string requestType, string url, string pathTranslated)


public void ReleaseHandler

Then in the Web Application, we change the config.xml file to forward ASMX requests to our own handler factory.

    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" 

        type="Devusion. Services.AutoDiscoveryWebServiceHandlerFactory"/>

Custom web service

Next we create our own web service, not the normal ASMX file, but a normal C# class file.

namespace Devusion.CustomControls
    [WebService(Namespace = "<a href="%22"></a>")]
    public class Service : WebService
        [ScriptMethod(UseHttpGet = true)]
        public string HelloWorldYou(string name)
            return DateTime.Now.ToLongTimeString()+ ": Hello " + name;

The WebServiceHandlerFactory

Now let's write the code that makes it possible to call /Devusion.CustomControls.Service.asmx. This will call our AutoDiscoveryWebServiceHandlerFactory.GetHandler function. Here we have to translate the URL to the Type of the class. We do this by looping over the loaded assemblies and checking for web service attributes. This should be optimized for better performance. If we find no matching type, we will return null.

private Type GetServiceType(string TypeName)
    // TODO: Caching mechanism for assembly checks
    foreach (
        Assembly loadedAssembly in AppDomain.CurrentDomain.GetAssemblies())
        Type ClassType = loadedAssembly.GetType(TypeName);
        if (ClassType != null)
            return ClassType;
    return null;

In Gethandler, we write:

// Try to get the type associated with the request (On a name to type basis)
Type WebServiceType = 

// if we did not find any send it on to the 
// original ajax script service handler.
if (WebServiceType == null)
    // [REFLECTION] Get the internal class 
    // System.Web.Script.Services.ScriptHandlerFactory create it
    IHttpHandlerFactory ScriptHandlerFactory = 
    UsedHandlerFactory = ScriptHandlerFactory;
    return ScriptHandlerFactory.GetHandler(
        context, requestType, url, pathTranslated);

The code here will get the WebServiceType. If no type is found, it will forward the request to the default Ajax Framework ScriptHandlerFactory, using reflection because it is a internal class. Almost all functionality for the ScriptHandlerFactory that normally handles the web service and Javascript proxy are either private or internal. Note that most of the functionality you will see from here on was found using the tool. The web service handler can be accessed with 3 different requests:

  1. A normal web service Method request (/Devusion.CustomControls.Service.asmx?......)
  2. A Javascript Proxy Generation request (/Devusion.CustomControls.Service.asmx/js or /jsdebug)
  3. A Javascript method Call ( /Devusion.CustomControls.Service.asmx/method )

First, we check for a normal or Javascript request:

// [REFLECTION] get the Handlerfactory : RestHandlerFactory
// (Handles Javascript proxy Generation and actions)
IHttpHandlerFactory JavascriptHandlerFactory = 

// [REFLECTION] Check if the current request is a Javasacript request
// JavascriptHandlerfactory.IsRestRequest(context);
System.Reflection.MethodInfo IsScriptRequestMethod = 
    BindingFlags.Static | BindingFlags.NonPublic);
if ((bool)IsScriptRequestMethod.Invoke(null, new object[] { context }))
    // Javascript Request
    // Normal Request

Then for the Javascript request we check for a proxy request or a method request:

// Check and see if it is a Javascript Request or a request for a 
// Javascript Proxy.
bool IsJavascriptDebug = 
    string.Equals(context.Request.PathInfo, "/jsdebug", 
bool IsJavascript = string.Equals(context.Request.PathInfo, "/js", 
if (IsJavascript || IsJavascriptDebug)
    // Proxy Request
    // Method Request

Now for actions in the 3 request types.

The JavaScript proxy request

Here we do a lot more reflection; every reflection is explained in the comments. We create a WebServiceData with WebServiceType, which will then contain all of the information needed to create the web service. We directly use this object in the GetClientProxyScript method, which is a method of the object WebServiceClientProxyGenerator. It then returns the Javascript proxy.

// [REFLECTION] fetch the constructor for the WebServiceData Object
ConstructorInfo WebServiceDataConstructor = AjaxAssembly.GetType(
    BindingFlags.NonPublic | BindingFlags.Instance, null, 
    new Type[] { typeof(Type), typeof(bool) } , null);

// [REFLECTION] fetch the constructor for the WebServiceClientProxyGenerator
ConstructorInfo WebServiceClientProxyGeneratorConstructor = 
    BindingFlags.NonPublic | BindingFlags.Instance, null, 
    new Type[] { typeof(string), typeof(bool) }, null);

// [REFLECTION] get the method from WebServiceClientProxy to 
// create the Javascript : GetClientProxyScript
MethodInfo GetClientProxyScriptMethod = 
    "GetClientProxyScript", BindingFlags.NonPublic | BindingFlags.Instance, 
    null, new Type[] { AjaxAssembly.GetType(
    "System.Web.Script.Services.WebServiceData") }, null);

// [REFLECTION] We invoke : 
// new WebServiceClientProxyGenerator(url,false).GetClientProxyScript(
new WebServiceData(WebServiceType));
string Javascript = (string)GetClientProxyScriptMethod.Invoke(
    new Object[] { url, IsJavascriptDebug }), new Object[] 
            new object[] { WebServiceType, false })

Then for a little caching, this was almost literally taken from the original unit with Reflector.

// Check the assembly modified time and use it as caching http header
DateTime AssemblyModifiedDate = GetAssemblyModifiedTime(

// See "if Modified since" was requested in the http headers, 
// and check it with the assembly modified time
string s = context.Request.Headers["If-Modified-Since"];

DateTime TempDate;
if (((s != null) && DateTime.TryParse(s, out TempDate)) && (
    TempDate >= AssemblyModifiedDate))
    context.Response.StatusCode = 0x130;
    return null;

// Add HttpCaching data to the http headers
if (!IsJavascriptDebug && (
    AssemblyModifiedDate.ToUniversalTime() < DateTime.UtcNow))
    HttpCachePolicy cache = context.Response.Cache;

Then we need to get the Javascript Proxy String back to the client trough an HTTPHandler.

internal class JavascriptProxyHandler : IHttpHandler
    string Javascript = "";

    public JavascriptProxyHandler(string _Javascript)
        Javascript = _Javascript;
    bool IHttpHandler.IsReusable { get { return false; } }
    void IHttpHandler.ProcessRequest(HttpContext context)
        context.Response.ContentType = "application/x-Javascript";

Then we create and return in in the GetHandler code:

HttpHandler = new JavascriptProxyHandler(Javascript);
return HttpHandler;

That's it for the Javascript proxy. Let's get on with the other, simpler two.

The JavaScript method request

When you use the methods generated in the Javascript Proxy, it will access the web service with /methodname. The reason this handler is different from the normal web service handler (SOAP/XML), is that this interface needs to translate between Javascript JSON in our C# Methods.

IHttpHandler JavascriptHandler = 

// [REFLECTION] fetch the constructor for the WebServiceData Object
ConstructorInfo WebServiceDataConstructor = AjaxAssembly.GetType(
    BindingFlags.NonPublic | BindingFlags.Instance, null, 
    new Type[] { typeof(Type), typeof(bool) } , null);

// [REFLECTION] get method : JavaScriptHandler.CreateHandler
MethodInfo CreateHandlerMethod = JavascriptHandler.GetType().GetMethod(
    "CreateHandler", BindingFlags.NonPublic | BindingFlags.Static, null, 
    new Type[] { AjaxAssembly.GetType(
    "System.Web.Script.Services.WebServiceData"), typeof(string) }, null);

// [REFLECTION] Invoke CreateHandlerMethod :
// HttpHandler = JavaScriptHandler.CreateHandler(WebServiceType,false);
HttpHandler = (IHttpHandler)CreateHandlerMethod.Invoke(
    JavascriptHandler, new Object[]
        new object[] { WebServiceType, false }), 
return HttpHandler;

We create the JavascriptHandler and, like in the previous handler, the WebServiceData. Then we call CreateHandler with the WebServiceData as Parameter, which returns the correct HTTPHandler.

Normal web service request

The normal request is simpler. The class is public and only the function that receives WebServiceData instead of a filepath is private. We use this function to get another HttpHandler and return it.

// Remember the used factory for later in ReleaseHandler
IHttpHandlerFactory WebServiceHandlerFactory = new WebServiceHandlerFactory();
UsedHandlerFactory = WebServiceHandlerFactory;

// [REFLECTION] Get the method CoreGetHandler
MethodInfo CoreGetHandlerMethod = UsedHandlerFactory.GetType().GetMethod(
    "CoreGetHandler", BindingFlags.NonPublic | BindingFlags.Instance);

// [REFLECTION] Invoke the method CoreGetHandler :
// WebServiceHandlerFactory.CoreGetHandler(WebServiceType,context,
    context.Request, context.Response);
HttpHandler = (IHttpHandler)CoreGetHandlerMethod.Invoke(UsedHandlerFactory, 
    new object[]
    WebServiceType, context, context.Request, context.Response
return HttpHandler;


Now to see it all in action. First, we add a web service reference to the ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server" >
        <asp:ServiceReference Path="Devusion.CustomControls.asmx" />

Below it, we put:

<script type="text/Javascript">
    function RunHelloWorld(name)
        name = prompt("Please enter your name :","John Doe");
    function HelloWorldDone(result,context,senderFunction)
    function WebServiceCallFailed(error,context,senderFunction)
<input type="button" onclick="RunHelloWorld();" value="Click Here" />

You can now place a breakpoint in the HelloWorldYou method in the WebService class in the service.cs file. Just run the page and let the magic of reflection do the rest! In this example, I did not implement the actual custom control or the preferred script# implementation of all the Javascript in combination with some Silverlight. I will divulge on that in future articles.

Demo code

The attached ZIP is a complete demo solution. It should open and build directly, so let me know if there are any problems. The code was created using Visual Studio 2005 Professional and ASP.NET Ajax 1.0.


  • 30 May, 2007 - Original version posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Robertjan Tuit
Founder Devusion
Netherlands Netherlands
Robertjan is an independent Developing and usability Consultant.

He has been a speaker at CodeCamp, TechDays and other venues. And blogs regularly on

His specialaties are User Interfaces, Silverlight, WPF, Custom Controls and Software Architecture, Design and Agile development.

You may also be interested in...


Comments and Discussions

QuestionGreat job! Pin
mctd30-Nov-11 7:11
membermctd30-Nov-11 7:11 
GeneralSublime! Pin
jimiscott28-Jan-11 7:13
memberjimiscott28-Jan-11 7:13 
GeneralGreat job Pin
Akleinek21-Feb-10 23:57
memberAkleinek21-Feb-10 23:57 
QuestionLicensing Pin
Ruh Chauhan25-Nov-08 12:05
memberRuh Chauhan25-Nov-08 12:05 
AnswerRe: Licensing Pin
Robertjan Tuit25-Nov-08 12:08
memberRobertjan Tuit25-Nov-08 12:08 
GeneralBest Article In a Long Time Pin
rodmj20-May-08 10:03
memberrodmj20-May-08 10:03 
GeneralRe: Best Article In a Long Time Pin
Jorge Bay Gondra5-Sep-08 3:42
memberJorge Bay Gondra5-Sep-08 3:42 
GeneralExcellent, thanks Pin
pdelprat22-Jan-08 2:39
memberpdelprat22-Jan-08 2:39 
GeneralRe: Excellent, thanks Pin
Robertjan Tuit22-Jan-08 2:48
memberRobertjan Tuit22-Jan-08 2:48 
GeneralRe: Excellent, thanks Pin
pdelprat22-Jan-08 3:54
memberpdelprat22-Jan-08 3:54 
GeneralRe: Excellent, thanks Pin
pdelprat22-Jan-08 3:32
memberpdelprat22-Jan-08 3:32 
Generalextremely useful Pin
James Ashley27-Dec-07 10:35
memberJames Ashley27-Dec-07 10:35 
GeneralVery Nice Pin
naytron197511-Jun-07 10:48
membernaytron197511-Jun-07 10:48 
GeneralGreat (Was in need for that 1.5 years ago) Pin
Martin Bohring31-May-07 0:04
memberMartin Bohring31-May-07 0:04 
GeneralRe: Great (Was in need for that 1.5 years ago) Pin
Robertjan Tuit5-Jun-07 23:36
memberRobertjan Tuit5-Jun-07 23:36 
GeneralGreat! Pin
Steven Berkovitz30-May-07 10:37
memberSteven Berkovitz30-May-07 10:37 
GeneralRe: Great! Pin
Robertjan Tuit5-Jun-07 23:33
memberRobertjan Tuit5-Jun-07 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 30 May 2007
Article Copyright 2007 by Robertjan Tuit
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid