0

I am registering OwinContext in StructureMap:

container.Register(() => HttpContext.Current.GetOwinContext());

But I get the following error on AssertConfigurationIsValid:

No owin.Environment item was found in the context

I found a solution for SimpleInjector which is the following:

container.RegisterPerWebRequest(() => {
   if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying()) {
     return new OwinContext();
   }
   return HttpContext.Current.GetOwinContext();
});

Can this be replicated with StructureMap 3? I can't find a container.IsVerifying in it ...

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

0

If your context isn't being resolved during the container's registration then you can defer the resolving and execution of your dependency in StructureMap by using injecting your type in a delegate such as Func<>.

The dependency will then only be resolved when you invoke the function.

...
public YourController(Func<HttpContextWrapper> context)
{
    this.context = context;
}

public ActionResult Index()
{
    var contextWrapper = this.context(); // dependency is resolved at this point.
}
...
Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63