Accessing services from module initializers

Topics: Web Client Software Factory
Feb 12, 2007 at 9:05 PM
Hi all!
I want to access HttpContextLocatorService from one of my module's module initializer class. In "RegisterSiteMapInformation" method, I need to get a reference to HttpContextLocatorService and call GetGlobalResourceObject method to get the site map item's text in a language other than English.

How should I do this?

Thanks in advance...
Feb 12, 2007 at 10:34 PM
IHttpContextLocatorService will have already been registered as a global service to the application so all you need to do is request it.

If you only need it in the default RegisterSiteMapInformation Method, just pass it to the method just like the ISiteMapBuilderService.

In your ModuleInitializer Class you could do the following in the Load Method:

public override void Load(ICompositionContainer moduleContainer)
    // ...
    RegisterSiteMapInformation(moduleContainer.Services.Get<ISiteMapBuilderService>(true), moduleContainer.Services.Get<IHttpContextLocatorService>(true));

and then change your RegisterSiteMapInformation Method to:

protected virtual void RegisterSiteMapInformation(ISiteMapBuilderService siteMapBuilderService, IHttpContextLocatorService httpContextLocatorService)
    IHttpContext context = httpContextLocatorService.GetCurrentContext();
    // ...

You will then have access to the IHttpContext Object.




David Hayden
Microsoft MVP C#
Feb 13, 2007 at 10:07 AM
Thanks for the info.

For globalization I need to call GetGlobalResourceObject method on the HttpContext class, IHttpContextLocatorService gives me the current context which doesn't let me access to global resources. I wrote another service that directly accesses to the HttpContext class and use it for globalization. I couldn't find a more elegant solution...