private void UnityWithConfiguration() { // Create and populate a new Unity container from configuration UnityConfigurationSection UnityConfigurationSectionObject = (UnityConfigurationSection)ConfigurationManager.GetSection("UnityBlockConfiguration"); IUnityContainer UnityContainerObject = new UnityContainer(); UnityConfigurationSectionObject.Containers["ContainerOne"].Configure(UnityContainerObject); Console.WriteLine("\nRetrieved the populated Unity Container."); // Get the logger to write a message and display the result. No name in config file. ILogger ILoggerInstance = UnityContainerObject.Resolve <ILogger>(); Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO Default UNITY!")); // Resolve an instance of the appropriate class registered for ILogger // Using the specified mapping name in the configuration file (may be empty for the default mapping) ILoggerInstance = UnityContainerObject.Resolve <ILogger>("StandardLoggerMappedInConfig"); // Get the logger to write a message and display the result Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO StandardLogger!")); // Resolve an instance of the appropriate class registered for ILogger // Using the specified mapping name (may be empty for the default mapping) ILoggerInstance = UnityContainerObject.Resolve <ILogger>("SuperFastLoggerMappedInConfig"); // Get the logger to write a message and display the result Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO SuperFastLogger!")); // Constructor Injection. // Resolve an instance of the concrete MyObjectWithInjectedLogger class // This class contains a reference to ILogger in the constructor parameters MyObjectWithInjectedLogger MyObjectWithInjectedLoggerInstance = UnityContainerObject.Resolve <MyObjectWithInjectedLogger>(); // Get the injected logger to write a message and display the result Console.WriteLine("\n" + MyObjectWithInjectedLoggerInstance.GetObjectStringResult()); // Throws error as we are trying to create instance for interface. //IMyInterface IMyInterfaceObject = UnityContainerObject.Resolve<IMyInterface>(); IMyInterface IMyInterfaceObject = UnityContainerObject.Resolve <IMyInterface>(); Console.WriteLine("\n" + IMyInterfaceObject.GetObjectStringResult()); //If we are not sure whether a named registration exists for an object, // we can use the ResolveAll method to obtain a list of registrations and mappings, and then check for the object we need in the list returned by this method. // However, this will cause the container to generate all the objects for all named registrations for the specified object type, which will affect performance. IEnumerable <object> IEnumerableObjects = UnityContainerObject.ResolveAll(typeof(ILogger)); int i = 0; foreach (ILogger foundObject in IEnumerableObjects) { // Convert the object reference to the "real" type ILogger theObject = foundObject as ILogger; i++; if (null != theObject) { Console.WriteLine(theObject.WriteMessage("Reading Object " + i.ToString())); } } UnityContainerObject.Teardown(IMyInterfaceObject); Console.ReadLine(); }