与温莎城堡组件的注册指定实例 [英] Specifying instance for registration of a component with Castle Windsor
问题描述
我有什么可能是在这里对温莎城堡,我还没有被使用很长这一个简单的问题。我想注册一个具体的实例,这将是单执行服务的服务。
I have what is probably a simple question here about Castle Windsor, which I haven't been using for very long. I'm trying to register a service with a specific instance that will be the singleton implementation of the service.
容器不能尝试创建服务本身,因为实施它具有的依赖关系,该容器将不能够解决。我有服务的一个实例,我想这是唯一用于任何请求服务实例。但我似乎有很少的运气
The container cannot try to create the implementation of the service itself because it has dependencies that the container will not be able to resolve. I have an instance of the service and I want that to be the one and only instance used for anyone requesting the service. But I appear to be having very little luck.
我暂时不得不通过使用此代码提出了希望:
I momentarily had hopes raised by using this code:
container.Register(Component.For<IMyInterface>().Instance(serviceObj));
但是,所有城堡的实例确实是它做一个.GetType()和注册类型。为服务请求将随之导致容器尝试创建该类型和故障时,它可以不填的依赖关系。
But all Castle does with the instance is do a .GetType() on it and register the type. Requests for the service will subsequently cause the container to try to create that type and fail when it can't fill the dependencies.
那么,有没有办法做我想在这里做什么?如果没有,我会创建某种的IServiceProvider那获取服务的实例,并没有依赖关系的容器填写。但是这种感觉就像多的工作比合适的解决方案围绕着。
So is there any way to do what I want to do here? If not I will create some kind of IServiceProvider that fetch the instance of the service and have no dependencies for the container to fill out. But this feels like more of a work around than the right solution.
有什么建议?谢谢!
推荐答案
尝试使用的 AddComponentInstance 容器的内核对象的方法。我想这是你所需要的
Try using the AddComponentInstance method on the container's Kernel object. I think this is what you need.
请注意:这种技术现在已经过时。使用 container.Register(Component.For< T>()实例(MYT));
,而不是(感谢eouw0o83hf)
Please note: This technique is now deprecated. Use container.Register(Component.For<T>().Instance(myT));
instead (thanks to eouw0o83hf)
这篇关于与温莎城堡组件的注册指定实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!