Best way to access IEmailTemplateService and IEmailSender

16 views
Skip to first unread message

Andre Loker

unread,
May 6, 2008, 3:11:24 PM5/6/08
to Castle Project Users
Hi,

I am looking for the best way to get access to some of the MonoRail
services. For example I need access to IEmailTemplateService and
IEmailSender outside of a Controller.

Preferably I'd like them to be injected as dependencies automatically
(I use Windsor integration), however they do not seem to be registered
as components.

So, what I do now is this:

//..........
public class SomeComponent: ISomeService {

public IEmailTemplateService Templates { get; set; }
public IEmailSender Sender { get; set; }

public SomeComponent() {
// wire up the dependencies manually :-(
var services =
MonoRailHttpHandlerFactory.CurrentEngineContext.Services;
Templates = services.EmailTemplateService;
Sender = services.EmailSender;
}
...
// .........
(In this case, SomeComponent is registered as a component in Windsor,
so automatic dependency injection of Templates and Sender would have
been nice)

My questions are:
1. Is there a way to have common monorail services like
IEmailTemplateService and IEmailSender be injected into components by
Windsor?
2. Is MonoRailHttpHandlerFactory the recommended starting point to
access the current engine context and its services?
3. How do I access the IMonoRailContainer? The
MonoRailHttpHandlerFactory.Container is not static (although the
backing field is).

Regards,
Andre

Roelof Blom

unread,
May 6, 2008, 3:24:13 PM5/6/08
to castle-pro...@googlegroups.com

Roelof Blom

unread,
May 6, 2008, 3:24:33 PM5/6/08
to castle-pro...@googlegroups.com
Andre, sorry for mispelling your name.

Victor Kornov

unread,
May 6, 2008, 3:29:46 PM5/6/08
to castle-pro...@googlegroups.com
simple sample from my current project:

public class GlobalApplication : HttpApplication, IMonoRailContainerEvents
    {
        public void Created(IMonoRailContainer container)
        {
        }

        public void Initialized(IMonoRailContainer container)
        {
            Register.MonorailServices(IoC.Container, container);
        }
    }
public class Register
{
        public static void MonorailServices(IWindsorContainer app, IMonoRailServices rails)
        {
            app.Register(
                Component
                    .For<IEmailTemplateService>()
                    .Instance(rails.EmailTemplateService),
                Component
                    .For<IEmailSender>()
                    .Instance(rails.EmailSender)
            );
        }
}

Victor Kornov

unread,
May 6, 2008, 3:34:32 PM5/6/08
to castle-pro...@googlegroups.com
Note that IMonoRailContainerEvents.Initialized "event" is NOT fired on Application_Start but on first request that is handled by Monorail (this is when everything is initialized). So, Monorail email services won't be accessible to your e.g. logging code to log errors on app_start.

Andre Loker

unread,
May 6, 2008, 3:43:19 PM5/6/08
to castle-pro...@googlegroups.com
Thanks to both of you, Roelof and Victor, for the fast and useful response. I overlooked IMonoRailContainerEvents as a starting point to hook up the services. Now everything is wired up nicely.

Regards,
Andre


Roelof Blom schrieb:
Reply all
Reply to author
Forward
0 new messages