I am trying to figure out how the NancyFx request container works, so I created a little test project.
I created a this interface
public interface INancyContextWrapper
{
NancyContext Context { get; }
}
With this implementation
public class NancyContextWrapper : INancyContextWrapper
{
public NancyContext Context { get; private set; }
public NancyContextWrapper(NancyContext context)
{
Context = context;
}
}
Then in the bootstrapper I register it like this
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
base.ConfigureRequestContainer(container, context);
container.Register<INancyContextWrapper>(new NancyContextWrapper(context));
}
I use this context wrapper in a class that does nothing but return the request url as a string.
public interface IUrlString
{
string Resolve();
}
public class UrlString : IUrlString
{
private readonly INancyContextWrapper _context;
public UrlString(INancyContextWrapper context)
{
_context = context;
}
public string Resolve()
{
return _context.Context.Request.Url.ToString();
}
}
And finally use this in a module
public class RootModule : NancyModule
{
public RootModule(IUrlString urlString)
{
Get["/"] = _ => urlString.Resolve();
}
}
When I do that the request is always null. Now I can more or less figure out that since IUrlString
isn't configured in the request container configuration, TinyIoc resolved INancyContextWrapper
at application startup before any request was made, and TinyIoc does not re-register dependencies that down the dependency graph relies on something configured in the request container configuration.
My question is what the best practice is for using ConfigureRequestContainer? Do I have to register everything that in any way relies on NancyContext explicitly in the request container configuration? That can very quickly become bloated and hard to maintain. I love how TinyIoc does assembly scanning, so having to do this is a bit of a setback.