我在StructureMap中注册OwinContext:
container.Register(() => HttpContext.Current.GetOwinContext());
但是我在AssertConfigurationIsValid上收到以下错误:
No owin.Environment item was found in the context
我找到了SimpleInjector的解决方案,如下所示:
container.RegisterPerWebRequest(() => {
if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying()) {
return new OwinContext();
}
return HttpContext.Current.GetOwinContext();
});
可以用StructureMap 3复制吗?我找不到容器。在其中验证......
答案 0 :(得分:0)
如果在容器注册期间没有解析您的上下文,那么您可以通过在Func<>
之类的委托中注入您的类型来推迟在StructureMap中解析和执行您的依赖项。
只有在调用函数时才会解析依赖关系。
...
public YourController(Func<HttpContextWrapper> context)
{
this.context = context;
}
public ActionResult Index()
{
var contextWrapper = this.context(); // dependency is resolved at this point.
}
...