Unity of Enterprise Library Problems

This post will take about what ever problems that might come into my way while using ms unity:

http://unity.codeplex.com

1. The following error: InvalidCastException

{“Unable to cast object of type
‘System.Configuration.DefaultSection’ to type
‘Microsoft.Practices.Unity.Configuration.UnityConfigurationSection’.”}

took 3 hours to be fixed with a very simple solution that is almost difficult to think about.

The main problem is that .config file is not written in the correct order of:

<?xml version=”1.0″ encoding=”utf-8″?>

<configuration>

<system.diagnostics>

..

.

</system.diagnostics>

 <configSections>

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

  </configSections>

</configuration>

The correct order is to set the “configSections” section imediatly after the “configuration” section:

<?xml version=”1.0″ encoding=”utf-8″?>

<configuration>

  <configSections>

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

</configSections>

  <system.diagnostics>

..

.

</system.diagnostics>

</configuration>

and now u can load it by:

        Dim fileMap as new ExeConfigurationFileMap() With { .ExeConfigFilename = “applicationFile.exe.config” }         Dim configuration as System.Configuration.Configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
Dim unitySection = DirectCast(configuration.GetSection(“unity”), UnityConfigurationSection)         Dim container as IUnityContainer = new UnityContainer()
container.LoadConfiguration(unitySection)

About Reader Man حسين ناجي الصفافير

حسين ناجي الصفافير Hussain Naji Al-Safafeer Info: https://readerman1.wordpress.com/aboutme
This entry was posted in Development, Technical and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s