asp.net mvc测试项目找不到windsor文件
你好,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使配置路径可配置,例如,
在您的应用程序中,configPath 是“Configuration\Windsor.config”,而在测试中,您将拥有类似“....\Configuration\Windsor.config”的路径。
请注意,您通常不应在测试中依赖容器,除非您正在运行一些集成测试。
另外,静态容器构建器似乎不是一个好主意,请查看Windsor Installers 以模块化方式注册您的组件。
Just make the config path configurable, e.g.
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.