I keep getting an error "System.Web.HttpException: Request is not available in this context" when creating my Kernel. Which makes sense given that one of my bindings has a dependency on the Context. Obviously no context should be present on Application_Start. I'm just wondering if anyone knows how to resolve this.
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();
}
}
Do your think, that access to the HttpContext
from Business
layer is good idea? Maybe you can spend some time on refactoring? For example, something like this .
From application perspective you can do something like this:
private void RegisterDependencyResolver()
{
kernel
.Bind<ISession>()
.To<SessionService>()
.InRequestScope()
.WithConstructorArgument(
"context",
ninjectContext => new HttpContextWrapper(HttpContext.Current)
);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.