This is a continuation of a blog series on creating services.  You can find the previous articles here:

So the ApplicationContext object uses a service loader as one way to load the IService objects into it.  This class exists solely to load these services into the context.  This object is then linked into the Application context through the configuration file.  The below example is used for loading MVC services by default (you can inherit, override this method, and add additional ones you use as a way to make it easier for loading the services you need).  This object already exists in the Nucleo.Web.Mvc DLL, in the Nucleo.Web.Context namespace.

public class MvcApplicationContextServiceLoader : IApplicationContextServiceLoader
{
 #region ” Methods “

 public virtual void LoadServices(IApplicationContextProvider provider)
 {
  provider.RegisterService<IApplicationStateService>(new MvcApplicationStateService());
  provider.RegisterService<IBrowserCapabilitiesService>(new WebFormsBrowserCapabilitiesService());
  provider.RegisterService<ICookieService>(new MvcCookieService());
  provider.RegisterService<INavigationService>(new MvcNavigationService());
  provider.RegisterService<IPostDataService>(new MvcPostDataService());
  provider.RegisterService<ISessionStateService>(new MvcSessionStateService());
  provider.RegisterService<IServerUtilityService>(new MvcServerUtilityService());
  provider.RegisterService<IUrlResolutionService>(new MvcUrlResolutionService());
 }

 #endregion
}

The LoadServices method is where the action happens.  The application context provider is used to receive these services and supply them to the ApplicationContext.  You don’t actually have to worry about that; the service provider is used by the context to serve up these services.  This class is then linked up in the configuration file.  See the following example for the setup:

 <configSections>
 <nucleo>
  <section name=”contextSettings” type=”Nucleo.Context.Configuration.ContextSettingsSection,Nucleo” />
 </nucleo>
</configSections>

<nucleo>
 <contextSettings contextLoaderType=”Nucleo.SampleClasses.Context.SampleApplicationLoader,Nucleo.OnlineTests” />
</nucleo>

The context settings section is the configuration object for specifying the loader you would like to use.  At runtime, the ApplicationContext object uses this to load the relevant services, so when you do:

context.GetService<ICookieService>();

A valid service will be returned to you.  We’ll talk more later about the purpose of the context provider later, how that’s configured .