I'm using NancyFX and Highway.Data + Entity Framework for an API project. My module has a dependency on a repository, which has a dependency on an DataContext. I need that DataContext to be registered and include the connectionstring from web.config, so I have this:
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoC.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
//Gotta specify how to register the DataContext to use the connectionstring
container.Register<IDataContext>(
(c, p) =>
new DataContext(ConfigurationManager.ConnectionStrings[1].ConnectionString,
c.Resolve<IMappingConfiguration>()));
base.ApplicationStartup(container, pipelines);
}
}
This sets up the registration so it uses my web.config connectionstring, but also uses whatever IMappingConfiguration was already registered by auto-registration.
But it looks like it's registering it as a singleton instead of per web request. This means that data gets cached between web requests, which is not what I want.
I've tried adding .AsMultiInstance() to the registration above, but then I get an error on startup: "Cannot convert current registration of TinyIoC.TinyIoCContainer+DelegateFactory to multi-instance"
Can anyone suggest how I can register this properly?
Container configurations should be performed in either ConfigureApplicationContainer
or ConfigureRequestContainer
depending on your life-time requirements.
Hope this helps