获取ScriptHandlerFactory处理程序 [英] Getting ScriptHandlerFactory handler
问题描述
有没有办法来调用System.Web.Script.Services.ScriptHandlerFactory类GetHandler方法,该方法返回的IHttpHandler类型的对象。我知道ScriptHandlerFactory是一个内部类,但therey从那里我能得到任何其他方式?我说的原因是因为我想我的路由路径的.asmx和路由需要这种类型的代码:
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));
}
}
以上WebServiceHandlerFactory使用,但我想用ScriptHandlerFactory只因为我想要使用jQuery的ajax来电了。我将呼吁通过jQuery AJAX调用ASMX方法。你能帮助我这样做?
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?
推荐答案
从Spring.Net借来的。我用它来访问 SimpleHttpHandler
因为我使用的IoC创建的HttpHandler
秒。
Borrowed from Spring.Net. I use it to access the SimpleHttpHandler
because I use IoC to create HttpHandler
s.
编辑:的这是我用来找出如何做到这一点的代码。显然,它需要的完全信任,然后我不喜欢这个技巧,但ASP.NET并没有很容易让我创造我自己的处理工厂,需要依靠内置的时候做的IoC 。-in功能
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.
编辑2:固定的完全限定的类型名称 System.Web.Services.Protocols。 WebServiceHandlerFactory
。从搜索的IHttpHandler
这是的System.Web
到的WebService 固定装配搜索code>这是
System.Web.Services
。让我知道,如果你有任何问题,
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.
修改3:对不起这一点。我离开了他们使用的 SecurityCritical
类,如果你愿意,你可以做的另一种方式,但我只是从他们那里直接拷贝时,我做到了。它是用来提升并断言权限,这就是为什么你需要的完全信任的
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.
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();
}
}
}
这篇关于获取ScriptHandlerFactory处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!