Welcome to AspAdvice Sign in | Join | Help
Dynamically Loading Modules in SCSF

The startup time of any Smart Client Application will increase with number of modules when we are loading all the views in the module controller. In such situations we can load the modules dynamically / on demand.

We need to use two built-in services for dynamic module loading.
ModuleLoaderService: This service is used to load the module whenever it is needed.
FileCatalogModuleEnumerator: This service is used to read an xml file (ProfileCatalog.xml) and retrieve the list of modules mentioned in that xml file.


In this example I will customize the ModuleController.cs of Infrastructure.Module project which will be under source folder of the Smart Client Solution.


Steps For Dynamic Module Loading:

1) Create an xml file(Dynamic.xml) in shell application which will contain the list of modules which should be loaded dynamically. Remove these module names from profile catalog.

Listing 1 – Dynamic.xml


<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">
     <Modules>
       <ModuleInfo AssemblyFile="Module1.dll" />
       <ModuleInfo AssemblyFile="Module2.dll" />
    </Modules>
</SolutionProfile>


2) Add a FileCatalogModuleEnumerator service to read above file. The following code needs to be written in AddServices method of ModuleController.cs in Infrastructure.Module project. 

Listing 2 - Adding Service to the Workitem

Private void AddServices()
        {
            WorkItem.Services.Add<FileCatalogModuleEnumerator>(new FileCatalogModuleEnumerator("Dynamic.xml"));
        }


3) Write a method for dynamic module loading.


The following method will load the module based on the index we pass.
 

Listing 3 - LoadModule method.


public void LoadModule(int ModuleIndex)
        {
            FileCatalogModuleEnumerator objFileCatalogModuleEnumerator = WorkItem.Services.Get<FileCatalogModuleEnumerator>();
            IModuleLoaderService objModuleLoaderService = WorkItem.Services.Get<IModuleLoaderService>();
            IModuleInfo[] modules= objFileCatalogModuleEnumerator.EnumerateModules();
            if (!CheckModuleLoaded(modules[ModuleIndex]))
                objModuleLoaderService.Load(WorkItem.RootWorkItem, modules[ModuleIndex]);
        }

4) The above method will call CheckModuleLoaded method.

The code for CheckModuleLoaded method is as follows

Listing 4 - CheckModuleLoaded method.

 

The following method will check whether a particular module is already loaded. If the module is already loaded it will return true otherwise false.

 

private bool CheckModuleLoaded(IModuleInfo module)

        {

            IModuleLoaderService objModuleLoaderService= WorkItem.Services.Get<IModuleLoaderService>();

            IList<LoadedModuleInfo> listLoadedModules;

            listLoadedModules = objModuleLoaderService.LoadedModules;

            foreach (LoadedModuleInfo loaded in listLoadedModules)

            {

                if (loaded.Assembly.ManifestModule.Name == module.AssemblyFile.ToString()) return true;

            }

            return false;

 

        }


Finally we need to call LoadModule method to load a specific module.

 

Posted: Saturday, December 08, 2007 10:18 AM by krishna kishore
Filed under:

Comments

Jagan Kumar said:

Thank you for this nice article I tried with you r code. I am getting exception "An exception occurred while enumerating the modules using the Microsoft.Practices.CompositeUI.Services.FileCatalogModuleEnumerator enumerator."
# July 9, 2008 5:01 AM

krishna kishore said:

please check for the appropriate xmlins of SolutionProfile in the following xml tag (both xml files). The version depends on the SCSF version you are using.

copy the value from the ProfileCatalog of your application

<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile/2.0">

# July 9, 2008 7:35 AM

Maxx said:

yes it crashes

you have to use the following syntax in cataloge.xml file to get it worked.

Looks like FileCatalogModuleEnumerator support the older syntax only

<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">

    <Modules>

      <ModuleInfo AssemblyFile="Module1.dll" />

      <ModuleInfo AssemblyFile="Module2.dll" />

   </Modules>

</SolutionProfile>

# July 17, 2008 8:53 AM

PhilW said:

Newby to SCSF Any chance of example locn for LoadModule() and use. Also what syntax actually works for the ...xml file. The Cataloge.xml i unknown to me
# July 24, 2008 1:34 AM

PhilW said:

Newby to SCSF Any chance Example of LoadModule call and its location within a module for that call. Also What is the format of Cataloge.xml? I am having trouble with an ModuleLoad Exception!
# July 24, 2008 1:42 AM

vandana said:

how can i unload dynamic loaded module ?
# December 22, 2009 1:40 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the code you see below

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS