Null error while Adding a Global service in a Foundational Module

Dec 17, 2008 at 4:30 PM
Hello,
I have a Foundational Module 'Reports' in which I would like to add global services.
I get a null reference error in the ModuleInitializer class at the line underlined below -- AddGlobalServices(container.Parent.Services). container.Parent is not set so its null.

Please see the code snippet below.

public override void Load(CompositionContainer container)<o:p></o:p>

{<o:p></o:p>

base.Load(container);<o:p></o:p>

AddGlobalServices(container.Parent.Services);<o:p></o:p>

AddModuleServices(container.Services);<o:p></o:p>

}

In the Web.config the module definition is a shown below:

  <compositeWeb>

    <modules>

      <module name="Shell" assemblyName="Shell" virtualPath="~/" />

      <module name="Reports" assemblyName="GSBAU.Reports">

        <dependencies>

          <dependency module="Shell" />

        </dependencies>

      </module>

      </modules>

  </compositeWeb>

 

 

 

 

I am not sure why the Parent propery of the container is not set to the RootContainer.
I appreciate any suggestions.

Thanks,
Saru

Dec 18, 2008 at 3:06 PM
Edited Dec 18, 2008 at 3:19 PM

Hi Saru,

 

The reason why the Parent property of the container in a Foundational Module is null is because it is the RootContainer (does not have any parent). Foundational Modules do not have a module specific container like the Business Modules, so they always use the RootContainer (you can only add Global Services). So, if you want to add Global Services from your Foundational Module you should use the container.Services property.

 

When you use the Guidance Package, and execute the “Add Foundational Module” recipe, the Load method of the ModuleInitializer class looks like the following:

 

public override void Load(CompositionContainer container)

{

base.Load(container);

 

AddGlobalServices(container.Services);

}

 

Based in your code snippet, it looks like you copied and refactored the code from a Business Module. You should not do that because when a Business Module is initialized by CWAB, a new child container is created (which has the Parent property set to the root container) and associated to the virtualPath of the module. Instead, when a Foundational Module is initialized by CWAB, like I said, the application’s root container is associated to it (which has the Parent property set to null).

 

Perhaps, you may want to try replacing your code with the one above.

 

Please, let me know if this helps.

 

Ezequiel Sculli

http://blogs.southworks.net/esculli/