ResolveAll method is not returning list of regitering class in container

Jan 11, 2011 at 3:51 PM

Hi ,

I am using ResolveAll method of UnityContainer class for creating list regitered class in Config File so that i can run the corresponding method of registered class. But ResolveAll is not creating List of objects. I am using unity 2.0.

Here is my code.

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;


namespace DemoProjForUnity
{
    public interface IDataBase
    {
        void Run();
    }
    public class SQLServer : IDataBase
    {


        void IDataBase.Run()
        {
            Console.WriteLine("This is SQL Server");
        }
    }
    public class Oracle : IDataBase
    {
        public void Run()
        {
            Console.WriteLine("This is Oracle.");
        }
    }
    public class ClsDataBase
    {
        private IDataBase database;
        public ClsDataBase(IDataBase _database)
        {
            this.database = _database;

        }     

        public void Run()
        {
            database.Run();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using(IUnityContainer container = new UnityContainer())
            {
                try
                {
                       container.LoadConfiguration();                      
                       IEnumerable<ClsDataBase> clsCust = container.ResolveAll<ClsDataBase>();
                       IEnumerator<ClsDataBase> enumerator = clsCust.GetEnumerator();
                       while (enumerator.MoveNext())
                       {
                           enumerator.Current.Run();
                       }                    
                   Console.ReadLine();
                   
                }
                catch (Exception Exc)
                {
                    Console.WriteLine(Exc.Message);
                    Console.ReadLine();
                }
            }
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
   <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </configSections>
 
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias
      alias="TraceSource"
      type="System.Diagnostics.TraceSource, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

    <assembly name="DemoProjForUnity"/>
    <namespace name="DemoProjForUnity"/>
    <container>
      <register type="IDataBase" mapTo="Oracle" name="Ora"/>
      <register type="IDataBase" mapTo ="SQLServer" name ="Sql"/>
    </container>
  </unity>
</configuration>
What i am doing mistake here? i am not able to get that.
Regards,
Gopal C
Jan 11, 2011 at 5:14 PM

You're registering IDataBase, but resolving ClsDataBase. From the code, it looks like you're halfway between two different designs and got stuck in the middle.

Change your resolve call to match the type you registered.