How to load unity settings from config file?

18,327

I was able to make it work. I have created a demo console application using the same namespaces you're using, as follows:

Assembly information

Nuget packages installed:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Unity" version="5.8.6" targetFramework="net461" />
  <package id="Unity.Abstractions" version="3.3.0" targetFramework="net461" />
  <package id="Unity.Configuration" version="5.2.3" targetFramework="net461" />
</packages>

App.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
    </configSections>

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <assembly name="UnityExample" />
        <namespace name="UnityExample.Service" />

        <containers>
            <container name="Service">
                <register type="IProductService" mapTo="ProductService"/>
            </container>
        </containers>
    </unity>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Unity.Configuration" publicKeyToken="6d32ff45e0ccc69f" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Code:

namespace UnityExample.Service
{
    public interface IProductService
    {

    }

    public class ProductService : IProductService
    {
    }
}

Main:

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

using Unity;
using UnityExample.Service;

namespace UnityExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(container, "Service");

            IProductService service = container.Resolve<IProductService>();
        }
    }
}
Share:
18,327

Related videos on Youtube

Mihai Alexandru-Ionut
Author by

Mihai Alexandru-Ionut

Husband, Father and Experienced Full Stack Software Engineer, focused on .NET technologies with a demonstrated history of working in the information technology and services industry. I consider programming an art and I improve my source-code until I no longer see the difference between profession and art. Bachelor and Master Degree in Computer Science, Babes-Bolyai University

Updated on June 04, 2022

Comments

  • Mihai Alexandru-Ionut
    Mihai Alexandru-Ionut over 1 year

    I have an unity configuration file(App.config) as below:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
      </configSections>
      <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <alias alias="IProductService" type="UnityExample.Service.IProductService, UnityExample.Service" />
        <containers>
          <container name="Service">
            <register type="IProductService" mapTo="ProductService"/>
          </container>
        </containers>
      </unity>
    </configuration>
    

    Now I want to load the configuration from above file.

    var container = new UnityContainer().LoadConfiguration("Service");
    

    But I received following error:

    [ArgumentNullException: Value cannot be null. Parameter name: section] Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName)

    • Rui Jarimba
      Rui Jarimba over 5 years
      Do you really need to use the configuration file to configure the dependencies? I find it easier to create a Boostrapper class and register the dependencies there - here's an example (ignore the DependencyResolver line, it's used in ASP.NET MVC applications): c-sharpcorner.com/UploadFile/dacca2/…
    • Mihai Alexandru-Ionut
      Mihai Alexandru-Ionut over 5 years
      @RuiJarimba, yes, I need to use the configuration file in order to load assemblies dynamically.
    • Rui Jarimba
      Rui Jarimba over 5 years
      Which version of Unity and .NET Framework are you using?
    • Mihai Alexandru-Ionut
      Mihai Alexandru-Ionut over 5 years
      @RuiJarimba, .NET 4.6.1 and Unity 5.8.6