Microsoft.Practices.Unity Error for Enterprise Library Logging??

Topics: Logging Application Block
Jun 5, 2012 at 9:09 PM

I have the following line of code in a class:

private LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();


When I start with debugging (F5), I get the following message: "File load exception was unhandled...   Could not load file or assembly 'Microsoft.Practices.Unity, Version=2.0.414.0,"  But Microsoft.Practices.Unity is not referenced for this class.  Why am I getting this message?

The entire code block looks like this:

using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Prism.Logging;
using Skeleton.Logging;

namespace Skeleton
  class Bootstrapper : UnityBootstrapper

    private LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

    #region Method Overrides

    /// <summary>
    /// Instantiates the Shell window.
    /// </summary>
    /// <returns>A new ShellWindow window.</returns>
    protected override DependencyObject CreateShell()
      /* This method sets the UnityBootstrapper.Shell property to the ShellWindow
       * we declared elsewhere in this project. Note that the UnityBootstrapper base 
       * class will attach an instance of the RegionManager to the new Shell window. */

      return new ShellWindow();

    protected override IModuleCatalog CreateModuleCatalog()
      var moduleCatalog = new DirectoryModuleCatalog();
      moduleCatalog.ModulePath = @".\Modules";
      return moduleCatalog;

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
      // Call base method
      var mappings = base.ConfigureRegionAdapterMappings();
      if (mappings == null) return null;

      //// Add custom mappings
      //   ServiceLocator.Current.GetInstance<RibbonRegionAdapter>());

      // Set return value
      return mappings;

    protected override void InitializeShell()
      // Log the start of the bootstrap method
      LogEntry log = new LogEntry();
      log.Message = string.Format("Bootstrap begins");
      log.Priority = Skeleton.Logging.Priority.Normal;



      App.Current.MainWindow = (Window)this.Shell;




Jun 5, 2012 at 9:37 PM

Enterprise Library uses Unity under the covers so it will need to be able to locate the Unity assemblies at runtime.

Randy Levy
Enterprise Library support engineer 

Jun 6, 2012 at 1:16 PM

I assume that you mean the Microsoft.Practices.Unity, Version=2.0.414.0 assembly should be in the solution bin when I press F5.  However, what I find there is an assembly with a different version.  The version in the bin is the same version (2.1.505.0) that is referenced by the project, and no other project in the solution references the Microsoft.Practices.Unity assembly.  From where is the runtime getting the reference to version 2.0.414.0?

Jun 7, 2012 at 5:59 AM

What version of Prism are you using?  Prism 4.0 uses Unity 2.0.414.0 whereas Prism 4.1 uses Unity 2.0.505.0.  Another place to check is config files that may have Version=2.0.414.0 set or Enterprise Library Version=5.0.414.0 set.

You could see if the Assembly Binding Log Viewer may shed some light on the issue (logging all binds might give a hint).

Randy Levy
Enterprise Library support engineer 

Jul 3, 2012 at 11:05 AM
Edited Jul 3, 2012 at 11:13 AM

I met the same problem when I use Unity 2.1 but logging of EntLib 5.0. It seems to be a problem of Service Locator, but I have already used "Microsoft.Practices.ServiceLocation.dll" in Unity 2.1 bin folder. Any suggestion except to replace Unity 2.1 back to Unity 2.0 embeded in EntLib 5.0?

Jul 4, 2012 at 4:44 AM

This has been solved. It seems to be a compatibility conflict between EntLib 5.0 and Unity 2.1. The solution is to update Enterprise Library 5.0 to Enterprise Library 5.0 optional update 1, if you are using Unity 2.1.

Jul 4, 2012 at 8:56 PM
Edited Jul 4, 2012 at 9:01 PM

I am already using Unity 2.1 with Enterprise Library 5.0 optional update 1. 

I worked around this problem by restoring my project from a backup previous to the appearance of the error and then recoded the missing parts of my project by simply copying and pasting.  The problem did not reappear in the project's newest version.  I no longer receive the message,  "File load exception was unhandled... Could not load file or assembly 'Microsoft.Practices.Unity, Version=2.0.414.0", but I still don't know why it happened in the first place.