NancyFX:如何使用引导程序来持久化对象 [英] NancyFX: How to use bootstrapper to persist an object

查看:76
本文介绍了NancyFX:如何使用引导程序来持久化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,有时想在发布/获取文件中访问.我不想每次按发布/获取路线将其加载为特定于应用程序时加载它.我认为我应该加载一个对象以将数据一次存储在bootstrapper中,并根据需要进行引用,但是找不到任何具体示例-如何实现此目标?

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?

推荐答案

您可以读取XML文件,并将结果粘贴到应用程序启动期间在容器中注册的某些对象中.然后,您的模块可以注入该对象.

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;
    }
  }

这篇关于NancyFX:如何使用引导程序来持久化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆