Tag Archives: .Net

CAB – How to enumerate modules..

So, couple of weeks ago, maybe a bit more time, since this last period my free time tends to be very hectic, I admit mostly because of my mistakes, so there were quite long periods when haven’t do any programming at all. Anyway since I cannot do anything normally like other peoples do, and (ironic) because CAB seems not hard enough to grasp at first sight, quite at the beginning of my journey I faced a problem. Due to the nature of the application, it is not possible to load the modules from a catalog file, so I had to find out a new way to enumerate the modules. Here enters the IModuleEnumerator interface.

Turns out, CAB is well written, and is quite easy to replace various services with your own custom services. It turned out it is quite easy to replace the module enumerator service also. All you have to do is to define a class implementing the IModuleEnumerator interface:

public class MyModuleEnumeratorService : IModuleEnumerator
{
    public GenesisModuleEnumeratorService(yourparams)
    {
      [ handle yourparams ]
    }

    #region IModuleEnumerator Members

    public Microsoft.Practices.CompositeUI.Configuration.IModuleInfo[]
        EnumerateModules()
    {
        [ implement here your module enumeration logic (retrieving
          list of modules from database,network or whatever place is
          required), store it into a IModuleInfo[] array and return
          it back to CAB ]
    }

    #endregion
}

After you have this class, you will need to change your shell code, to replace the standard module enumerator service with your new service. This might not be the right way to do it, but it seems to work. Id there is a better way, please let me known.

So, in your shell, add the code into AddServices() method:

protected override void AddServices()
{
    base.AddServices();

    RootWorkItem.Services.Remove(typeof(IModuleEnumerator));
    RootWorkItem.Services.Add(typeof(IModuleEnumerator),
        new MyModuleEnumeratorService(params));
}

Seems like first you have to remove the already existing service, then add the new one. Just adding the new one seems like does not replace the already existing service.

Since the service should be started when applications starts, you can either add it to app.config, or in my case, since I cannot use app.config either in order to configure my application, I just implemented the service in an assembly which is referenced from the shell. Seems to work, however if you know a better way to solve this, please let me known.

That’s all. Now you have your very own module (catalog) enumerator.