asp.net mvc测试项目找不到windsor文件

发布于 2024-09-08 23:39:07 字数 1040 浏览 2 评论 0原文

你好,我使用 Windsor 作为 DI 容器,

我的代码如下,

public static class ContainerBuilder
    {
        public static IWindsorContainer Build()
        {
            var container = new WindsorContainer("Configuration\\Windsor.config");



            // automatically register controllers
            container.Register(AllTypes
                                   .Of<Controller>()
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));

            container.Register(


               Component.For<IServiceLocator>().Instance(new WindsorServiceLocator(container)),
              Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHibernateRepository<>)).LifeStyle.Transient

               );


            return container;
        }
    }

我需要从测试项目中调用它,问题是当我这样做时,windsor.config 从未找到,并且测试似乎总是失败,哪里是放置此配置文件的最佳方法或者是否有更好的方法来执行此操作? 谢谢

Hi there Im using windsor as a DI container,

my code is below

public static class ContainerBuilder
    {
        public static IWindsorContainer Build()
        {
            var container = new WindsorContainer("Configuration\\Windsor.config");



            // automatically register controllers
            container.Register(AllTypes
                                   .Of<Controller>()
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));

            container.Register(


               Component.For<IServiceLocator>().Instance(new WindsorServiceLocator(container)),
              Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHibernateRepository<>)).LifeStyle.Transient

               );


            return container;
        }
    }

I need to call this from a test project , the problem is that when I do this the windsor.config is never found and the test seems to always fail, where is the best way to place this config file or is there a better approach to doing this?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-09-15 23:39:07

只需使配置路径可配置,例如,

public static IWindsorContainer Build(string configPath) {
  var container = new WindsorContainer(configPath);
  ...
}

在您的应用程序中,configPath 是“Configuration\Windsor.config”,而在测试中,您将拥有类似“....\Configuration\Windsor.config”的路径。

请注意,您通常不应在测试中依赖容器,除非您正在运行一些集成测试。

另外,静态容器构建器似乎不是一个好主意,请查看Windsor Installers 以模块化方式注册您的组件。

Just make the config path configurable, e.g.

public static IWindsorContainer Build(string configPath) {
  var container = new WindsorContainer(configPath);
  ...
}

In your app the configPath is "Configuration\Windsor.config" while in your tests you'll have a path like "....\Configuration\Windsor.config".

Note that you shouldn't generally depend on the container in your tests, unless you're running some integration tests.

Also a static container builder doesn't seem like a good idea, take a look at Windsor Installers to register your components in a modular way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文