Ninject:如何不在“;ashx”;文件,但仍然没有得到异常

本文关键字:异常 文件 何不 ashx Ninject | 更新日期: 2023-09-27 18:26:43

这是我用来在我的MVC3项目中实现依赖注入的方法

public class NinjectControllerFactory : DefaultControllerFactory
{
    private readonly IKernel _ninjectKernel;
    public NinjectControllerFactory()
    {
        _ninjectKernel = new StandardKernel();
        AddBindings();
    }
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return controllerType == null ? null : (IController)_ninjectKernel.Get(controllerType);
    }
    private void AddBindings()
    {
        _ninjectKernel.Bind<IUserRepository>().To<UserRepository>().InSingletonScope();            
    }
}

但我有一个巨大的问题,我想使用一个通用处理程序".ashx"来实现我的逻辑。

但是我得到了一个异常,因为httphandler不是控制器。

这里有一个例外:

    Server Error in '/' Application.
The IControllerFactory 'Infrastructure.NinjectFactory.NinjectControllerFactory' did not return a controller for the name 'registercustomer.ashx'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The IControllerFactory 'Infrastructure.NinjectFactory.NinjectControllerFactory' did not return a controller for the name 'registercustomer.ashx'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: The IControllerFactory 'Infrastructure.NinjectFactory.NinjectControllerFactory' did not return a controller for the name 'registercustomer.ashx'.]
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +422803
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8971636
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.547 

现在的问题是:我如何实现围绕这个bug的工作,对我来说,能够使用HttpHandler,并且仍然在我的项目中使用Ninject?

提前谢谢。

Ninject:如何不在“;ashx”;文件,但仍然没有得到异常

由于HttpHandler是由框架创建的,并且没有钩子或工厂方法来拦截ashx文件的创建,ninject无法创建此对象。

但是,您可以使用来自ashx的服务定位器调用或属性注入来请求来自ashx代码的依赖关系。但据我所知,ashx必须有一个默认的构造函数,然后你可以通过服务定位器(不太喜欢的方法)或通过属性注入从构造函数内部(或任何地方)解决依赖关系,就像这样:

public class Handler
{
    [Inject]
    public IService Service { get; set; }
}

EDIT:另外,要告诉mvc不要处理ashx文件,您需要添加这个来忽略路由:

routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");