Unity 3 and Error "The type name or alias "xxxxx" could not be resolved. Please check your configuration file and verify this type name."

Olivertech picture Olivertech · Sep 7, 2013 · Viewed 25.2k times · Source

Is there any way to resolve this problem with Unity 3 ?

I have made all that is possible to bypass this message error, but I can't resolve; I have already did everything I've seen in googles searches.

I am almost giving up and trying another DI solution.

My configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="Biblioteca" />
    <assembly name="Biblioteca.Contracts" />
    <assembly name="Biblioteca.Business" />
    <namespace name="Biblioteca" />
    <namespace name="Biblioteca.Contracts" />
    <namespace name="Biblioteca.Business" />
      <container>
        <register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" />
      </container>
  </unity>
</configuration>

My interface:

using Biblioteca.Transport;
using System.Linq;

namespace Biblioteca.Contracts
{
    public interface IManterCategoriaBO
    {
        IQueryable<CategoriaDTO> GetAll();
        CategoriaDTO GetById(int id);
        void Insert(CategoriaDTO dto);
    }
}

My concrete class:

using Biblioteca.Contracts;
using Biblioteca.Transport;
using Biblioteca.Data;
using System;
using System.Linq;

namespace Biblioteca.Business
{
    public class ManterCategoriaBO : IManterCategoriaBO
    {
        public CategoriaDTO GetById(int id)
        {
            CategoriaDTO dto = new CategoriaDTO();
            ManterCategoriaDO categoriaDO = new ManterCategoriaDO();

            dto = categoriaDO.GetById(1);

            return dto;
        }

        public IQueryable<CategoriaDTO> GetAll()
        {
            throw new NotImplementedException();
        }

        public void Insert(CategoriaDTO dto)
        {
            throw new NotImplementedException();
        }
    }
}

My Global.asax:

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Biblioteca.Dependency;

namespace Biblioteca
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //Below is a static variable to take the unity container
            //which is on a dependency project
            Global.Container = Bootstrapper.Initialise();
        }
    }
}

My Bootstrapper class:

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.Web.Mvc;
using Unity.Mvc4;

namespace Biblioteca
{
    public static class Bootstrapper
    {
        public static IUnityContainer Initialise()
        {
            var container = BuildUnityContainer();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            return container;
        }

        private static IUnityContainer BuildUnityContainer()
        {
            string path = ConfigurationManager.AppSettings["UnityConfigFilePath"].ToString();

            var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = path + "\\Unity.config" };

            System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

            //*** this line is firing the error !!! ****
            var container = new UnityContainer().LoadConfiguration(unitySection);

            return container;
        }
    }
}

My Dependency project static class:

using Microsoft.Practices.Unity;

namespace Biblioteca.Dependency
{
    public static class Global
    {
        public static IUnityContainer Container = null;

        public static T Resolve<T>()
        {
            return Container.Resolve<T>();
        }
    }
}

My UI model class file on MVC 4 project. I am using 4.5 framework.

using Biblioteca.Contracts;
using Biblioteca.Dependency;

namespace Biblioteca.Models
{
    public class LivroModel
    {
        public void GetAll()
        {
            if (Global.Container != null)
            {
                var categoriaBO = Global.Resolve<IManterCategoriaBO>();
                categoriaBO.GetById(1);
            }
        }
    }
}

I think everything is in the right way. But, I can´t see this DI works cause I got an error just in the mapping process, in line below on my Bootstrapper class, BuildUnityContainer method:

var container = new UnityContainer().LoadConfiguration(unitySection);

The error is:

The type name or alias Biblioteca.Contracts.IManterCategoriaBO could not be resolved. Please check your configuration file and verify this type name.

I have double checked all my classes and for me, they are ok. Or is it missing anything ?

Answer

TylerOhlsen picture TylerOhlsen · Sep 7, 2013

The problem is in you config file. You are mixing two concepts with some incorrect syntax.

The <assembly... /> and <namespace ... /> nodes provide an assembly and namespace search order when your <register ... /> node contains a type that cannot be found by itself. If a type cannot be found, it searches through all combinations of [namespace].Type, [assembly]. Here's where the error is: it does NOT search for Type, [assembly]. If any <namespace ... /> nodes are defined, it does NOT try appending only the assembly.

So your <register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" /> node has the type Biblioteca.Contracts.IManterCategoriaBO which does not contain the assembly, so it cannot be found. Therefore, it needs to do a search. You did specify <namespace ... /> nodes, so it will first try Biblioteca.Biblioteca.Contracts.IManterCategoriaBO, Biblioteca. Notice the duplicate Biblioteca name.

Here's a corrected config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="Biblioteca" />
    <assembly name="Biblioteca.Contracts" />
    <assembly name="Biblioteca.Business" />
    <namespace name="Biblioteca" />
    <namespace name="Biblioteca.Contracts" />
    <namespace name="Biblioteca.Business" />
    <container>
      <register type="IManterCategoriaBO" mapTo="ManterCategoriaBO" />
      <!-- Or this works -->
      <!--<register type="Biblioteca.Contracts.IManterCategoriaBO, Biblioteca" mapTo="Biblioteca.Business.ManterCategoriaBO, Biblioteca" />-->
    </container>
  </unity>
</configuration>