Error in xaml page when resolving LogWriter object

Topics: Logging Application Block, Silverlight Integration Pack
Mar 14, 2012 at 11:10 AM
Edited Mar 14, 2012 at 12:33 PM

I am using Silverlight 4 and doing logging using the Silverlight Integration Pack.

I have a silverlight class library called 'Framework' inside which there is a MessageLog.cs file which has following code:

using Microsoft.Practices.EnterpriseLibrary.Logging.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace SomeNamespace
    public class MessageLog
        LogEntry entry;
        LogWriter writer;

        // constructor
        public MessageLog()
            entry = new LogEntry();
            writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

    . . . 
// some code
    . . .

Included in the same project is a FBase.cs file which has following code:
namespace SomeNamespace 
    public class FBase : UserControl, INavigationAware 
        MessageLog logger; 
        public FBase() 
               logger = new MessageLog(); 
         . . . 
         // some code 
         . . . 

There is another Silverlight project in the same solution which refers to the above 'FrameWork' project.
This project has xaml files having code as below: 
    xmlns:d="" xmlns:mc="" 
    mc:Ignorable="d" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 
    d:DesignWidth="640" d:DesignHeight="480" Height="480" Width="800">

        <Loc:Org_Resource x:Key="Org_Resource_String"></Loc:Org_Resource>

    <Grid x:Name="grdOrganizationSetup" Background="White" Width="480">
. . . 
// some xaml code
. . . 

The solution gets built successfully and Logging is aslo working properly but when the 
.xaml file above is opened, it is not possible to see the design view for the xaml 
and it gives the following error:
'Cannot create instance of FBase' 
with following error details:
The type LogWriter cannot be constructed. You must configure the container to supply this value.
   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.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
When the line 'writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();' in constructor of MessageLog class is commented then
the error will not exist and the Design view for the xaml page can be seen. 
Has anyone faced such an issue? I have tried adding Microsoft.Practices.Unity.Silverlight.dll and Microsoft.Practices.Unity.Interception.Silverlight.dll
to the Framework project but it doesnt help.
I have been able to reproduce the issue in a small sample project. I can provide it if required.
Any help will be appreciated. Thanks.
Mar 16, 2012 at 9:13 AM

The issue is that the MainPage class is instantiating FBase.  At design time the Silverlight editor is trying to create the objects and is calling the constructor on FBase which eventually tries to instantiate a logger from the EnterpriseLibraryContainer.  This fails because the container is not set up.

I would recommend reading Troubleshooting Silverlight Designer Load Failures.

The workaround is to either enable the container to resolve the LogWriter object or to avoid attempting to resolve and instantiate a LogWriter at design time.  This could be done through design changes (avoid creating LogWriter objects (even indirectly) in the default constructor or determine if the code is running in design mode.  

Randy Levy
Enterprise Library support engineer