我制作了一个简单的OWIN中间件,它将为我提供一个用户对象并将其添加到HttpContext.Current.Items
,以便可用于每个请求的所有控制器和视图。在我自己的OWIN中间件中使用Ninject DI
这里是我的代码:
public class SetCurrentUserMiddleware : OwinMiddleware
{
public SetCurrentUserMiddleware(OwinMiddleware next) : base(next)
{
}
public override Task Invoke(IOwinContext context)
{
if (context.Request.User.Identity.IsAuthenticated)
{
// Do some work to get a userId... (omitted)
var repo = new UserRepository();
User user = repo.Get(userId);
HttpContext.Current.Items["CurrentUserContext"] = user;
}
return Next.Invoke(context);
}
}
我在我的web应用程序中使用Ninject - 我该如何重构这个中间件使我UserRepository注入的依赖?这可能吗?
您是否选择了任何实现。我也有这个问题 – Saravanan
我希望我不是在做一个愚蠢的问题,但是你是否试图将它绑定到你的IoC/DI容器中? '绑定()。();' –