gpt4 book ai didi

c# - Ninject:如何在 "ashx"文件中不使用 Ninject 并且仍然没有得到异常?

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:59 25 4
gpt4 key购买 nike

这就是我用来在我的 MVC3 项目中实现依赖注入(inject)的方法,

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 不是 Controller 。

这里是个异常(exception):

    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

现在的问题是:我如何实现解决此错误的工作,使我能够使用 HttpHandler 并继续在我的项目中使用 Ninject?

提前致谢。

最佳答案

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

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

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

编辑:另外,要告诉 mvc 不处理 ashx 文件,您需要添加此文件以忽略路由:

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

关于c# - Ninject:如何在 "ashx"文件中不使用 Ninject 并且仍然没有得到异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com