Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference XML mappings for Unity

The following code worked in version 3.5.1404, but is no longer working in version 5.0.1. Have the latest references via NuGet:

in web.config I have:

using Microsoft.Practices.Unity.Configuration;
using Unity;

namespace ACME.Core
{
    public static class UnityCommon
    {
        static IUnityContainer _container;

        /// <summary>
        /// Returns the Common Unity Container from memory, reloads if not loaded first
        /// </summary>
        /// <param name="forceRefresh">Forces the container to be reloaded in memory</param>
        /// <returns>IUnityContainer</returns>
        public static IUnityContainer GetContainer(bool forceRefresh = false)
        {
            if (_container == null || forceRefresh)
            {
                _container = new UnityContainer();
                _container.LoadConfiguration("Default"));
            }

            return _container;
        }
    }
}

This references XML mappings in web.config such as:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="ACME.Core" />
    <namespace name="ACME.Core.DTO" />

    <container name="Default">
      <register type="IStatusRepository" mapTo="StatusRepository" />
      <register type="INoteRepository" mapTo="NoteRepository" />
    </container>
</unity>

Error:

System.Configuration.ConfigurationErrorsException occurred HResult=0x80131902 Message=An error occurred creating the configuration section handler for unity: Could not load file or assembly 'Microsoft.Practices.Unity.Configuration' or one of its dependencies. The system cannot find the file specified. Source=Unity.Configuration StackTrace: at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, String containerName)

at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError) at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord) at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)

Do I need to call the XML configuration differently?

BTW, I tried it with just _container.LoadConfiguration(); and received the same error.

like image 799
Josh Avatar asked Dec 19 '22 03:12

Josh


1 Answers

Figured it out with help from contributor at Github (ENikS).

I was using the old namespace issue:

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />

But once I used the new one you provided it worked:

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
like image 51
Josh Avatar answered Dec 29 '22 11:12

Josh