nRouteApplicationService - Starting up services

Aug 21, 2010 at 8:16 PM

In all the examples/blogs/discussion - the nRouteapplicationService is launched directly in the App.xaml (for Silverlight applications). 

I have a TCP/IP Socket Listener (the Service) which has to launch on application launch and has to be remain active for the duration of the application. The Service has a MapService attribute as Singleton.

The following questions follow from the above:

1) How is the Service intiated when the Silverlight Application launches?

2)  nRouteApplicationService has a StartService(ApplicationServiceContext) method - is this where the Service is started? How is this method accessed.

3) Can the Service be iniated in the App.Xaml LifeTimeServices together with the nRouteApplicationService?

4) Or, does the NrouteApplicationService have to launched in code?



How do I start up this service from with the nRouteApplicationService, if indeed it is possible? Does the nRouteApplicationService have to be started in code? Or can it be done in 

Aug 21, 2010 at 9:36 PM
Edited Aug 21, 2010 at 9:38 PM

Well, I think the answer you are looking for lies in setting the InitializationMode on the service. The (nRoute.Components.Composition.)InitializationMode enum has two values, one OnDemand, which basically allows for lazy loading and the other option is WhenAvailable, which means as and when all the dependencies are available - so the later is what you need to use. So basically just do this, and nRoute will start it for you:


[MapService(typeof(IMyService), InitializationMode = InitializationMode.WhenAvailable, Lifetime=InstanceLifetime.Singleton)]
public class MyService : IMyService
 	// implement IMyService


Now about your questions, the important thing to understand is that nRouteApplicationService implements two special interfaces in SL called IApplicationLifetimeAware and IApplicationService - and when you add the nRouteApplicationService in the application lifetime objects collection (in app.xaml) it receives notifications about the application lifetime from SL as it progresses and we use those to notifications to start the assembly mapper and RLF. The ApplicationServiceContext method comes from IApplicationService, but really you don't need to worry about this, however see for more IApplicationLifetimeAware, IApplicationService and ApplicationLifetimeObjects in SL.

Now, any service should start quite early in the lifetime of the application, however if you want specific notifications as the application loads there is a facility for that.. try something like:


[MapChannelObserver(typeof(ApplicationStateInfo), InitializationMode = InitializationMode.WhenAvailable, Lifetime=InstanceLifetime.Singleton)]
public class LifetimeAwareClass : IObserver<ApplicationStateInfo>
    public void OnNext(ApplicationStateInfo value)
        // get your application state info            

    public void OnCompleted()
        // will not happen

    public void OnError(Exception exception)
        throw new NotImplementedException();


Also, if you want you can start nRouteApplicationService in code, just make sure you do it early and add it to the Application's ApplicationLifetimeObjects collection. You could also create you service (implement either of the two interfaces) and add it to SL's ApplicationLifetimeObjects collection.