NancyFX: How to use bootstrapper to persist an object

时间秒杀一切 提交于 2020-01-24 10:37:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!