3

Is there any way to call System.Web.Script.Services.ScriptHandlerFactory class GetHandler method which return IHttpHandler type object. I know ScriptHandlerFactory is an internal class but is therey any other way from where I can get it? The reason I am saying is because I want to route my .asmx paths and routing needs this type of code:

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
   return new WebServiceHandlerFactory().GetHandler(HttpContext.Current,
        "*",
        _VirtualPath,
        HttpContext.Current.Server.MapPath(_VirtualPath));
   }
}

Above "WebServiceHandlerFactory" is used but I want to use ScriptHandlerFactory only since I want to make use of jquery ajax calls too. I will be calling the asmx methods via jquery ajax calls. Can you help me in doing so?

Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
  • try inheriting from IHttpHandlerFactory? then you can do something like public virtual IHttpHandler GetHandler(HttpContext context, String requestType, String url, String pathTranslated) { return (IHttpHandler)aspxPage; } – Dmitry Savy Feb 15 '12 at 15:24
  • Dmitry, you can't inherit an interface. You can only implement it. Unfortunately, ASP.NET made a lot of classes internal, which is a huge pain. Like in my instance (see my answer), I needed the default functionality, but then I needed to be able to pass it to an IoC provider to build-up the properties. I couldn't do that without resorting to reflection. – Chris Benard Mar 08 '12 at 19:21

1 Answers1

1

Borrowed from Spring.Net. I use it to access the SimpleHttpHandler because I use IoC to create HttpHandlers.

Edit: This is the code that I used to figure out how to do it. Obviously, it requires full trust, and I didn't like this hack, but ASP.NET didn't make it easy for me to do IoC when creating my own handler factory that needed to rely on built-in functionality.

Edit 2: Fixed the fully qualified type name to System.Web.Services.Protocols.WebServiceHandlerFactory. Fixed the assembly search from searching for IHttpHandler which is in System.Web to WebService which is in System.Web.Services. Let me know if you have any issues.

Edit 3: Sorry about this. I left out the SecurityCritical class they use, which you can do another way if you want, but I just copied straight from them when I did it. It is used to elevate and assert permissions, which is why you need full trust.

Code

    static YourCodeHere()
    {
        PrivilegedCommand cmd = new PrivilegedCommand();
        SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), new SecurityCritical.PrivilegedCallback(cmd.Execute));
        handlerFactory = cmd.Result;
    }

    private class PrivilegedCommand
    {
        public IHttpHandlerFactory Result = null;

        public void Execute()
        {
            Type handlerFactoryType = typeof(System.Web.Services.WebService).Assembly.GetType("System.Web.Services.Protocols.WebServiceHandlerFactory");
            Result = (IHttpHandlerFactory)Activator.CreateInstance(handlerFactoryType, true);
        }
    }

/// <summary>
/// Utility class to be used from within this assembly for executing security critical code 
/// NEVER EVER MAKE THIS PUBLIC!
/// </summary>
/// <author>Erich Eichinger</author>
internal class SecurityCritical
{
    internal delegate void PrivilegedCallback();

    [SecurityCritical, SecurityTreatAsSafe]
    [MethodImpl(MethodImplOptions.NoInlining)]
    internal static void ExecutePrivileged(IStackWalk permission, PrivilegedCallback callback)
    {
        permission.Assert();
        try
        {
            callback();
        }
        finally
        {
            CodeAccessPermission.RevertAssert();
        }
    }
}
Community
  • 1
  • 1
Chris Benard
  • 3,167
  • 2
  • 29
  • 35