温莎城堡财产注入
我是温莎菜鸟,在依赖注入工作时遇到一些问题。我正在使用 ASP.NET Web 应用程序。
我已经完成了以下操作
public interface IHandler{
...
}
public class Handler : IHandler{
...
}
,然后我尝试在 global.asax application_start 中注册代码
container.Register(Component
.For(typeof(IHandler))
.ImplementedBy(typeof(Handler))
.Named("handler"));
当我想使用处理程序时,我创建了一个属性,
public IHandler handler{get;set;}
但是当我尝试使用它时,它为空?为什么?我错过了什么吗?
最好的问候
更新
我唯一要注册/解决的事情如下:
container.Register(Component
.For(typeof(IHandler))
.ImplementedBy(typeof(Handler))
.Named("handler"));
以及:
container.Resolve<IHandler>();
我需要做其他事情吗,运行此 att 应用程序启动是否有效?
更新2
问题是否会因为我尝试在 ascx 控件上进行依赖注入而发生?
Im a windsor noob and im having some problems getting dependency injection to work. im using a asp.net web application.
I have done the following
public interface IHandler{
...
}
public class Handler : IHandler{
...
}
then i try to register the code in global.asax application_start
container.Register(Component
.For(typeof(IHandler))
.ImplementedBy(typeof(Handler))
.Named("handler"));
When i want to use the Handler i create a property
public IHandler handler{get;set;}
but when i try to use it, it is null? why? am i missing someting?
Best regards
UPDATE
The only thing i doto register/resolve is the following:
container.Register(Component
.For(typeof(IHandler))
.ImplementedBy(typeof(Handler))
.Named("handler"));
and:
container.Resolve<IHandler>();
Do i need to do something else, Does it work to run this att application start?
UPDATE 2
Can the problem ocour because im trying to dependency inject on an ascx controll?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保具有 IHandler 属性的组件也在 Windsor 中注册(并解析)。
您说这是针对 ASP.NET 应用程序的。 Windsor组件的默认生活方式是单例。您确定要这样共享该组件吗?您可能希望此组件具有瞬态或每个 Web 请求的生活方式。
Make sure the component that has the IHandler property is also registered (and resolved) in Windsor.
You said this is for an ASP.NET application. The default lifestyle of Windsor components is singleton. Are you sure you want this component shared like that? You may want a transient or per-web-request lifestyle for this component.
尝试从注册中删除名称,如下所示:
或者,如果您必须命名组件,则可以对使用类使用
ServiceOverrides
:Try removing the name from the registration, like this:
Alternatively, if you must name the component, you can use
ServiceOverrides
for the consuming class:如果您要注册多个接口/服务,那么我建议按约定进行注册(文档中建议这样做)。考虑一下:
此方法根据类型名称和接口名称执行匹配。详细信息按约定注册组件
If you are going to be registering several interfaces/services, then I recommend registering by convention (this is recommended in the docs). Consider this:
This method performs matching based on type name and interface's name. More info Registering Components By Convention