问题
I have an XML file I want to access sometimes in post/get's. I dont want to have to load it up every time I hit the post/get route as its application specific. I think I should be loading up an object to store my data once in bootstrapper and referring to this as I need, but can't find any specific examples - how to achieve this?
回答1:
You can read the XML file and stick the result in some object that you register in the container during application start up. Your modules can then have that object injected.
That is; something like this in your bootstrapper:
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
var myXmlCacheInstance = ... // read your xml file and create an object to hold it
container.Register<MyXmlCahce>(myXmlCacheInstance);
}
}
and like this in your modules:
public class HomeModule : NancyModule
{
public HomeModule(MyXmlCache xmlCache)
{
Get["/"] => xmlCache;
}
}
来源:https://stackoverflow.com/questions/20360280/nancyfx-how-to-use-bootstrapper-to-persist-an-object