How does Stock Trader RI inject logging block into container?

Topics: Enterprise Library Core, General discussion, Logging Application Block, Silverlight Integration Pack
Mar 1, 2012 at 2:47 AM

Now I can only get LogWriter by EnterpriseLibraryContainer.Current.GetInstance,it will generates an InvalidOperationException if I regard LogWriter as a constructor parameter without configuration, how does Stock Trader RI inject LogWriter into container?

InvalidOperationException.Message is:The type LogWriter cannot be constructed. You must configure the container to supply this value.

InvalidOperationException.StackTrace is:

   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor)   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey)   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)   at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)   at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)   at BuildUp_Client.Shell.ShellModule(IBuilderContext )   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)

Mar 1, 2012 at 3:34 AM

In the Stock Trader RI project Enterprise Library is configured by downloading a XAML file asynchronously from the web server on application startup.

On Application_Startup the configuration files are downloaded.  Then in AsyncXamlConfigMerger the EnterpriseLibraryContainer is configured:

        private void ConfigureEnterpriseLibrary()
        {
            var configSource = DictionaryConfigurationSource.FromDictionary(this.mergedConfiguration);
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
        }

 This thread also might help since it talks about various Silverlight configuration options.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Mar 1, 2012 at 3:40 AM

The configuration can only be used for EnterpriseLibraryContainer, not UnityContainer of Prism.

Mar 1, 2012 at 5:07 AM

EnterpriseLibraryContainer internally uses a UnityContainer.  The bootstrapper, StockTraderRIBootstrapper, is resolved from the EnterpriseLibraryContainer.  The StockTraderRIBootstrapper constructor accepts an IUnityContainer instance which is injected automatically when the bootstrapper is created by the EnterpriseLibraryContainer (which is internally using a UnityContainer).  So the bootstrapper is injected with the internal IUnityContainer that EnterpriseLibrary is using.

The end result of this is that the internal EnterpriseLibraryContainer is the same instance as the IUnityContainer that is used by Prism.

If the container has been properly configured then the following should be equivalent:

EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
container.Resolve<LogWriter>();

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Mar 2, 2012 at 3:25 AM

Dear Randy Levy, you are so nice! And I also noticed that the UnityBootstrapper.CreateContainer should be overridden.