IOC Container dependency failure

Dec 11, 2014 at 6:28 PM
Edited Dec 13, 2014 at 8:01 PM
Hey everyone. I'm getting an error while using Gallio to debug a unit test with vb.net code. The error does not occur when the program I'm trying to test is debugged using SOAPUI to pass the input in, only during unit testing with gallio.

Set Up System.TypeInitializationException: The type initializer for 'CP.Adapter.Tests.ServiceIOCSetup' threw an exception. ---> PA.DPW.Practices.CompositeBlock.Module.ModuleLoadException: Failed to load module from assembly PA.DPW.eCIS.ServiceGateway.Cis, Version=17.0.0.0, Culture=neutral, PublicKeyToken=null. Error was:
Failed to load module from assembly PA.DPW.eCIS.ServiceGateway.Cis, Version=17.0.0.0, Culture=neutral, PublicKeyToken=null. Error was:
Resolution of the dependency failed, type = "PA.DPW.eCIS.ServiceGateway.Cis.CisEventListener", name = "(none)".
Exception occurred while: while resolving.

Exception is: InvalidOperationException - The current type, PA.DPW.eCIS.Root.Core.Interfaces.ILoggingService, is an interface and cannot be constructed. Are you missing a type mapping?

This is the class that the unit test is calling in it's setup section. It starts with the Shared Sub New() constructor but throws the error when it tries loader.Load(container).

Friend Class ServiceIOCSetup
' Make sure we cannot create instances of this class.

Private Sub New()

End Sub

Shared Sub New()

    Dim unityContainer As IUnityContainer = New UnityContainer()

    AddExtensions(unityContainer)

    Dim container As ICompositionContainer = New UnityCompositionContainer(unityContainer)

    LoadServices(container)

    LoadModules(container)

    IocContainerHelper.RootContainer = container

End Sub

''' <summary>

''' This is the Main Entry point to register types 

''' in the container.

''' </summary>

''' <remarks>        

'''  This method works as a bootstrapper in kicking off the

''' static constructor for the first call. Subsequent calls to this

''' method does nothing.

''' </remarks>

Public Shared Sub EnsureServices()



End Sub

''' <summary>

''' Method to add extensions to the container

''' </summary>

''' <param name="container"></param>

''' <remarks></remarks>

Private Shared Sub AddExtensions(ByVal container As IUnityContainer)

    container.AddNewExtension(Of SimpleEventBrokerExtension)()

    container.AddNewExtension(Of Interception)()

End Sub

''' <summary>

''' Load the individual Modules

''' </summary>

''' <remarks></remarks>

Private Shared Sub LoadModules(ByVal container As ICompositionContainer)



    container.RegisterInstance(GetType(IServiceLoaderService), New ServiceLoaderService())

    Dim loader As New AppModuleLoaderService()

    loader.Load(container)

End Sub

''' <summary>

''' Load all the services used by the BWCs and DAO layers

''' </summary>

''' <remarks></remarks>

Private Shared Sub LoadServices(ByVal container As ICompositionContainer)

    container.RegisterInstance(Of IValidationService)(New ValidationService())

    container.RegisterInstance(Of ITracingService)(New TracingService)

    'container.RegisterInstance(Of ILoggingService)(New LoggingService())

    'container.RegisterInstance(Of ILoggingService)("AsyncLoggingService", New AsyncLoggingService)

    container.RegisterInstance(Of IExceptionHandler)(New DefaultExceptionHandler)

    container.RegisterInstance(Of IHttpContextLocatorService)(New HttpContextLocatorService())

    container.RegisterInstance(Of IReferenceTableEnumParser)(New PA.DPW.eCIS.Shell.ReferenceTableEnumParser(New PA.DPW.eCIS.Itasca.RefTableUtility.Parsers.ReferenceTableEnumParser()))

    container.RegisterInstance(Of IReferenceTableService)(New ReferenceTableService())

    container.RegisterInstance(Of IOperationContextStorageProvider)(New EnterpriseServicesOperationContextStorageProvider())

    container.RegisterInstance(Of IAuthorizationOperationContextService)(New AuthorizationOperationContextService(container.Resolve(Of IOperationContextStorageProvider)()))

    container.RegisterInstance(Of IOperationContextMessageStorageService)(New OperationContextMessageStorageService(container.Resolve(Of IOperationContextStorageProvider)()))

    container.RegisterInstance(Of IAuthorizationProvider)(New DefaultAuthorizationProvider())

    container.RegisterInstance(Of INavigationService)(container.Resolve(Of WebNavigationService))

    Dim navigationWorkflow As New CommonFramework.Navigator.Runtime.NavigationServices("NAV_DRIVER")

    container.RegisterInstance(Of INavigationPathFactory)(New NavigationPathFactory(navigationWorkflow))

    container.RegisterInstance(Of INavigationWorkflowScheduler)(New NavigationWorkflowScheduler(navigationWorkflow))

    container.RegisterInstance(Of ISecurityContextLocatorService)(New SecurityContextLocatorService(New ConfigurableSecurityContext()))

    container.RegisterInstance(Of ICaseWorkflowScheduler)(New CaseWorkflowScheduler(navigationWorkflow))

    container.RegisterInstance(Of ISiteMapBuilderService)(New SiteMapBuilderService())

    container.RegisterInstance(Of IValidationService)(New ValidationService())

    container.RegisterInstance(Of ITracingService)(New TracingService)

    'container.RegisterInstance(Of ILoggingService)(New LoggingService)

    'container.RegisterInstance(Of ILoggingService)("AsyncLoggingService", New AsyncLoggingService)

    container.RegisterInstance(Of IExceptionHandler)(New DefaultExceptionHandler)

    ' Navigation-related services

    container.RegisterInstance(Of INavigationService)(container.Resolve(Of WebNavigationService))

    ' Reference table-related services

    container.RegisterInstance(Of IReferenceTableService)(New ReferenceTableService())

    container.RegisterInstance(GetType(IReferenceTableEnumParser), New ReferenceTableEnumParser(New PA.DPW.eCIS.Itasca.RefTableUtility.Parsers.ReferenceTableEnumParser()))

    '  container.RegisterInstance(Of ISecurityContextLocatorService)("ConfigurableSecurityContextService", New SecurityContextLocatorService(New ConfigurableSecurityContext()))

    container.RegisterInstance(Of IAuthorizationProvider)(New DefaultAuthorizationProvider())

    'Operation Context related services

    container.RegisterInstance(Of IOperationContextStorageProvider)(New WebOperationContextStorageProvider())

    container.RegisterInstance(Of ICustomCacheStorageProvider)(New CachingApplicationBlockStorageProvider())

    container.RegisterInstance(Of IOperationContextStorageProvider)(New WebOperationContextStorageProvider())

    container.RegisterInstance(Of ICachingResultService)(New UserLevelCachingService(container.Resolve(Of ICustomCacheStorageProvider)()))

End Sub
Once it is done with all this, when not debugging and failing, it calls a translator class which is meant to run this constructor, but if you don't do all of the IOC stuff above, it will throw an error saying that the IOC container is null, hence why I need it to complete calling the class above. If anyone has any answers, I'd appreciate it.

Private _enumParser As IReferenceTableEnumParser
    Friend Sub New()

        _enumParser = IocContainerHelper.Resolve(Of IReferenceTableEnumParser)()

    End Sub

*Update:

One of my coworkers figured it out. In the unit test we used this code in the setup section. I think this basically does what you were suggesting randy. Thanks for help.

Private _enumParser As IReferenceTableEnumParser
Private _container As UnityCompositionContainer

Private translator As CompassPreAppTranslator

<SetUp()> _

Public Sub SetUp()

    _enumParser = New Global.PA.DPW.eCIS.Shell.ReferenceTableEnumParser(New PA.DPW.eCIS.Itasca.RefTableUtility.Parsers.ReferenceTableEnumParser())

    _container = New UnityCompositionContainer(New UnityContainer)

    PA.DPW.eCIS.Root.Shared.IocContainerHelper.RootContainer = _container

    _container.RegisterInstance(Of IReferenceTableEnumParser)(_enumParser)

    translator = New CompassPreAppTranslator

End Sub
Dec 12, 2014 at 3:55 AM
The error message is telling you that ILoggingService is not registered. So, either it was not registered (posted code does show a registration) or the container reference that the ILoggingService was registered in is not being used.

What you can do is to initialize the container during FixtureSetUp (or SetUp depending on how you have structured your tests) and keep a reference to the container in an instance variable or set a service locator to use that container (since it looks like IocContainerHelper is a service locator). Then during the tests the initialized container will be used to resolve dependencies.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by JMarano on 12/13/2014 at 1:01 PM