Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Global.asax中创建绑定依赖于HttpContext的Ninject内核_C#_.net_Ninject_Global Asax_Httpcontext - Fatal编程技术网

C# 在Global.asax中创建绑定依赖于HttpContext的Ninject内核

C# 在Global.asax中创建绑定依赖于HttpContext的Ninject内核,c#,.net,ninject,global-asax,httpcontext,C#,.net,Ninject,Global Asax,Httpcontext,我一直收到一个错误System.Web.HttpException:在创建内核时,请求在此上下文中不可用。考虑到我的一个绑定依赖于上下文,这是有道理的。显然,应用程序启动时不应存在上下文。我只是想知道是否有人知道如何解决这个问题 public class NinjectBindings : NinjectModule { public override void Load() { //Framework Bind<IJsonLaye

我一直收到一个错误System.Web.HttpException:在创建内核时,请求在此上下文中不可用。考虑到我的一个绑定依赖于上下文,这是有道理的。显然,应用程序启动时不应存在上下文。我只是想知道是否有人知道如何解决这个问题

public class NinjectBindings : NinjectModule
{
    public override void Load()
    {     
        //Framework
        Bind<IJsonLayer>().To<JsonLayer>();
        Bind<IBusinessLayer>().To<BusinessLayer>();

        //Controllers
        Bind<ITeamController>().To<TeamController>();
    }
}


public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        ReflectionUtility.Kernel = new StandardKernel(new NinjectBindings());
    }
}


public class ReflectionUtility
{
    private static IKernel _kernel;

    public static IKernel Kernel {
        set { _kernel = value; }
    }
}



public class JsonLayer : IJsonLayer
{
    private readonly ITeamController _teamController;
    private readonly IBusinessLayer _businessLayer;

    public JsonLayer(ITeamController teamController, IBusinessLayer businessLayer)
    {
        _teamController = teamController;
        _businessLayer = businessLayer;
    }
}


public class BusinessLayer : IBusinessLayer
{
    //this is a super-simplification of what's going on. there are multiple different calls to HttpContext.Current.Request in this class
    public BusinessLayer()
    {
         //This is where it breaks
         var sessionUserId = HttpContext.Current.Request.Headers["X-SessionUserId"];
    }

}

public class TeamController : ITeamController
{
      public void DeleteTeam(int intTeam)
      {
          throw new NotImplementedException();
      }
}

您认为从业务层访问HttpContext是个好主意吗?也许你可以花点时间在重构上?例如,类似于

从应用程序的角度来看,您可以执行以下操作:

private void RegisterDependencyResolver()
{
     kernel
     .Bind<ISession>()
     .To<SessionService>()
     .InRequestScope()
     .WithConstructorArgument(
         "context", 
         ninjectContext => new HttpContextWrapper(HttpContext.Current)
      );

     DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}

我不会对HttpContext建立依赖关系,就在您设置一个变量来包含注入的服务时,我会将HttpContext分配给一个属性。这只是因为它让生活变得容易多了。我不知道为什么这会被否决,但这是解决这个问题的正确方法。您不希望业务层中存在具体的依赖关系。创建一个抽象!