Inject App Settings using Windsor

此生再无相见时 提交于 2019-11-30 08:38:32

I came up with a solution for this eventually based on hints from various sources on the web. The end result though involved pretty much copying three classes from Windsor verbatim and modifying them just a little bit. The end result is up on codeplex for your enjoyment.

http://windsorappcfgprops.codeplex.com/

I originally wrote this code quite some time ago so it's based on Windsor 1.0.3 - yes, it took me that long to get around to publishing the result!

The code allows you to have this in your app.config (or web.config, obviously):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="theAnswer" value="42"/>
  </appSettings>
</configuration>

...and access it from your Windsor XML config file like this:

<?xml version="1.0" encoding="utf-8" ?>
<castle>
  <components>
    <component
      id="answerProvider"
      service="Acme.IAnswerProvider, Acme"
      type="Acme.AnswerProvider, Acme"
      >
      <parameters>
        <theAnswer>#{AppSetting.theAnswer}</theAnswer>
      </parameters>
    </component>
  </components>
</castle>

There's a working example in the solution.

I wrote a post about a similar case a couple of months ago. It uses a SubDependencyResolver to inject the appropriate parameters. In your case, you can just change DynamicConfigurationSettings for ConfigurationManager.

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