如何使用Windsor容器将appSettings项的值(来自app.config或web.config)注入服务中?如果我想将Windsor属性的值注入服务中,则应执行以下操作:
< properties> < importantIntegerProperty> 666< / importantIntegerProperty> < / properties> < component id = myComponent service = MyApp.IService,MyApp type = MyApp.Service,MyApp > < parameters> < importantInteger>#{importantIntegerProperty}< / importantInteger> < / parameters> < / component>但是,我真正想做的是取#{importantIntegerProperty} 从应用程序设置变量中定义,如下所示:
< appSettings> < add key = importantInteger value = 666 /> < / appSettings>编辑:进行澄清;我意识到,使用温莎和 David Hayden,这本来就不可能 sliderhouserules 所指的文章实际上是关于他自己的(戴维海顿的)IoC容器,而不是温莎。 p>
我肯定不是第一个遇到这个问题的人,所以我想知道别人是如何解决这个问题的?
解决方案我最终基于网络上各种来源的提示提出了一个解决方案。尽管最终结果涉及从温莎逐字复制几乎三个类并对其进行一些修改。最终结果取决于Codeplex,您可以享受。
http ://windsorappcfgprops.codeplex/
我最初是在一段时间之前编写此代码的,所以它基于Windsor 1.0.3-是的,它花了我那很久才发布结果!
该代码允许您将其包含在app.config(或web.config)中,显然):
<?xml version = 1.0 encoding = utf-8吗? < configuration> < appSettings> < add key = theAnswer value = 42 /> < / appSettings> < / configuration>...并从您的Windsor XML配置文件中访问它,如下所示:
<?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>解决方案中有一个有效的示例。
How can I inject the value of an appSettings entry (from app.config or web.config) into a service using the Windsor container? If I wanted to inject the value of a Windsor property into a service, I would do something like this:
<properties> <importantIntegerProperty>666</importantIntegerProperty> </properties> <component id="myComponent" service="MyApp.IService, MyApp" type="MyApp.Service, MyApp" > <parameters> <importantInteger>#{importantIntegerProperty}</importantInteger> </parameters> </component>However, what I'd really like to do is take the value represented by #{importantIntegerProperty} from an app settings variable which might be defined like this:
<appSettings> <add key="importantInteger" value="666"/> </appSettings>EDIT: To clarify; I realise that this is not natively possible with Windsor and the David Hayden article that sliderhouserules refers to is actually about his own (David Hayden's) IoC container, not Windsor.
I'm surely not the first person to have this problem so what I'd like to know is how have other people solved this issue?
解决方案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.
windsorappcfgprops.codeplex/
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.
更多推荐
使用Windsor注入应用程序设置
发布评论