0

I want to inject a class into my module using the IOC framework in NancyFX, and I want that class to have access to the Context, just like the module does.

How would I do that?

The module has a property called Context, that doesn't seem to be injected (nor should it be, as it is the request state)... how does that work, and is that thread safe?

Thanks

Jay Pete
  • 4,123
  • 4
  • 35
  • 51

1 Answers1

0

Assuming you use the DefaultNancyBootstrapper as a base for your bootstrapper you register your dependency in your container on a per request basis by overriding ConfigureRequestContainer:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
    {
        container.Register(new MyDependency(context));
    }
}

If you use a different container, the implementation will differ slightly.

Christian Horsdal
  • 4,914
  • 23
  • 24
  • Hi Christian, thanks for the tip. I can get this to work. But I was hoping, there was some sort of assembly discovery, that registered everything that has a NancyContext in the constructor. Kind of like the application container does. – Jay Pete Feb 23 '15 at 19:09
  • Alternatively you could register the NancyContext in the request container. – Christian Horsdal Feb 23 '15 at 20:56
  • Registering NancyContext directly in the container doesn't work. But it was a good idea. I just wrapped the context, and registered the wrapper. That mean I only have to register the wrapper, and then everything else is done using the application container. Thanks for the input! – Jay Pete Feb 23 '15 at 22:33