如何实例基于与温莎城堡的web.config文件中的类? [英] How to instantiate a class based on web.config file with Castle Windsor?

查看:109
本文介绍了如何实例基于与温莎城堡的web.config文件中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC应用程序,我想根据写在web.config设置实例化一个类。

I'm working on an ASP.NET MVC application and I would like to instantiate a class based on settings written in web.config.

<configSections>
    <section name="castle"
        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>

<castle>
    <components>
        <component
            id="SqlRepository"
            service="GomokuGameAppDomain.IGameRepository, GomokuGameAppDomain"
            type="GomokuGameAppDomain.SqlGameRepository, GomokuGameAppDomain"
            lifestyle="PerWebRequest">
            <parameters>
                 <connectionString>Data Source=ZOLIQ-PC\SQLEXPRESS;Initial Catalog=Gomoku;Integrated Security=True</connectionString>
            </parameters>
    </component>
</castle>
...

我想用这样的:

var container =
    new WindsorContainer (new XmlInterpreter (new ConfigResource ("castle")));

gameRepository = container.Resolve (typeof (IGameRepository)) as IGameRepository;

我怎么能解决这个问题?先谢谢了。

How could I resolve this? Thanks in advance.

推荐答案

只是为了完整起见,整个code是这样的:

Just for the sake of completeness, the whole code looks like this:

var container = new WindsorContainer();
container.Install(Castle.Windsor.Installer.Configuration.FromAppConfig());
var gameRepository = container.Resolve<IGameRepository>();

快乐编码。

这篇关于如何实例基于与温莎城堡的web.config文件中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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