Getting ScriptHandlerFactory handler

早过忘川 提交于 2019-12-10 10:07:41

问题


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?


回答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();
        }
    }
}


来源:https://stackoverflow.com/questions/6865728/getting-scripthandlerfactory-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!