I need help for registering components in web app

34 views
Skip to first unread message

mynkow

unread,
Mar 22, 2012, 7:34:00 AM3/22/12
to castle-pro...@googlegroups.com
Hi people,

I have a web application with internal EventBus. The registration of the component is like this:

Component.For<IEventBus>().ImplementedBy<InProcessEventBus>().OnCreate((kernel, eventBus) =>
                {
                    (eventBus as InProcessEventBus).RegisterAllHandlersInAssembly(Assembly.GetAssembly(typeof(IHaveEventHandlers)), type => kernel.Resolve(type));
                }
As you can see each handler is initialized as a singleton when the Event Bus is created. The IWindsorContainer instance is initialized in global.asax.

Not the problem.
I have an external facility which registers additional event handlers. Right now I am doing it wrong. I resolve IEventBus instance within the facility and call the RegisterAllHandlersInAssembly(...) method.
The facility is called with all other component registrations in global.asax
Obviously this is wrong and I get an exception that the castle http module is not initialized because we are still in global.asax. Can you give me a hint how this should be done correctly? What concept am I missing here.

Best regards


Simon Laroche

unread,
Mar 22, 2012, 9:03:36 AM3/22/12
to castle-pro...@googlegroups.com
Hi Mynkow,

One way we do this is to have the InProcessEventBus have a dependency on IKernel and resolve the dependencies when we publish to the bus.

I'm not sure why you have a facility for this, facilities are use the extend de container, registrations should be done with an Installer.

Simon



--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/QdFPy_DdnQEJ.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

mynkow

unread,
Mar 22, 2012, 9:22:16 AM3/22/12
to castle-pro...@googlegroups.com
Hello Simon

I have a facility because the additional handlers are coming from a plugin.

Is there any other way because I really do not want to resolve handlers on publish. The handlers are always the same and they are initialized upfront because this is a web application. For winforms is other story.

10x


On Thursday, March 22, 2012 3:03:36 PM UTC+2, Simon wrote:
Hi Mynkow,

One way we do this is to have the InProcessEventBus have a dependency on IKernel and resolve the dependencies when we publish to the bus.

I'm not sure why you have a facility for this, facilities are use the extend de container, registrations should be done with an Installer.

Simon

Simon Laroche

unread,
Mar 22, 2012, 2:02:04 PM3/22/12
to castle-pro...@googlegroups.com
Can you share your facility code?

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.

mynkow

unread,
Mar 28, 2012, 2:56:39 AM3/28/12
to castle-pro...@googlegroups.com
Sure, nothing special there:

public class SearchFacility : AbstractFacility
    {
        protected override void Init()
        {
            Converter<IKernelSearchEngine> CreateSearchEngine = kernel =>
            {
                var systemSettings = kernel.Resolve<ISystemSettings>();
                SearchEnvironment.SetIndexPath(systemSettings.SearchIndexLocation);
                return new SearchEngine();
            };
 
            Kernel.Register(
                Component.For<SearchEngine>().UsingFactoryMethod(CreateSearchEngine),
                Component.For<IFluentSearch>().ImplementedBy<FluentSearch>().LifeStyle.Transient
                );
        }
    }

On Thursday, March 22, 2012 8:02:04 PM UTC+2, Simon wrote:
Can you share your facility code?

Simon Laroche

unread,
Mar 28, 2012, 12:11:11 PM3/28/12
to castle-pro...@googlegroups.com
Hi Mynkow

Where is the event bus registration code you were referring in your initial post in this facility?

If you want to resolve your Hanlders only once, why not you use a factory method.

            Converter<IKernel, IBus> CreateBus = kernel =>
            {
var hanlders =ResolveHandlers(kernel); //Resolve all handlers in the kernel return new InProcessBus(hanlders);
            };

Kernel.Register(                 Component.For<IBus>().UsingFactoryMethod(CreateBus)                                );

Put that in an installer. Just make sure you resolve the bus only once all handlers are registered.

Simon

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.

mynkow

unread,
Mar 30, 2012, 4:03:00 PM3/30/12
to castle-pro...@googlegroups.com
Well, I have main application installer where the core event handlers are registerd/resolved when the container creates IEventBus instance. The facility in my last post is from our search component. It may exists, it may not. So my best shot is to get an instance of that IEventBus within the facility and add the additional handlers. That facility is responsible for registering search event handlers, not the core.


On Wednesday, March 28, 2012 7:11:11 PM UTC+3, Simon wrote:
Hi Mynkow

Where is the event bus registration code you were referring in your initial post in this facility?

If you want to resolve your Hanlders only once, why not you use a factory method.

            Converter<IKernel, IBus> CreateBus = kernel =>
            {
var hanlders =ResolveHandlers(kernel); //Resolve all handlers in the kernel return new InProcessBus(hanlders);
            };

Kernel.Register(                 Component.For<IBus>().UsingFactoryMethod(CreateBus)                                );

Put that in an installer. Just make sure you resolve the bus only once all handlers are registered.

Simon


To post to this group, send email to castle-project-users@googlegroups.com.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages