Ninject:如何在“ashx”文件中不使用Ninject并且仍然没有例外?

时间:2012-03-06 05:50:24

标签: c# asp.net-mvc-3 ninject

这是我用来在我的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();            
    }
}

但我有一个很大的问题,我想使用Generic Handler和".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?

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

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

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

编辑:另外,告诉mvc不处理ashx文件你需要添加它来忽略路由:

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