Windsor를 사용하여 앱 설정을 주입합니다
-
05-07-2019 - |
문제
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>
편집하다: 명확히하기 위해; 나는 이것이 Windsor와 데이비드 헤이든 기사 저것 슬라이더 houserules 실제로 Windsor가 아닌 자신의 (David Hayden 's) IOC 컨테이너에 관한 것입니다.
나는이 문제가있는 첫 번째 사람이 아니기 때문에 다른 사람들 이이 문제를 해결 한 것은 어떻게해야합니까?
해결책
나는 웹의 다양한 소스의 힌트를 기반으로 결국 이것에 대한 솔루션을 생각해 냈습니다. 최종 결과는 Windsor Verbatim에서 세 가지 클래스를 거의 복사하고 약간 수정했습니다. 최종 결과는 당신의 즐거움을 위해 CodePlex에 있습니다.
http://windsorappcfgprops.codeplex.com/
나는 원래이 코드를 꽤 얼마 전에 썼다. 그래서 그것은 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>
솔루션에는 작업 예가 있습니다.
다른 팁
나 게시물을 썼습니다 몇 달 전에 비슷한 사건에 대해. subdependencyResolver를 사용하여 적절한 매개 변수를 주입합니다. 귀하의 경우 ConfigurationManager의 DynamicConfigUrationSettings 만 변경할 수 있습니다.