Unity 3 and Error "The type name or alias" xxxxx "cannot be resolved. Check the configuration file and check the name of this type."

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

I did everything possible to get around this error message, but I can’t solve it ... I’ve been trying to solve it for 2 days, and already did everything that I saw in the search for searches.

I almost give up and try another DI solution. Please any help would be ok ...

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 specific 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; } } } 

The static class of the project My Dependency:

 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 interface model class file in 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 correct. But I can’t see that this DI works because I only got an error during the matching process, in the line below my Bootstrapper class, the BuildUnityContainer method:

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

Mistake:

The type name or alias of Biblioteca.Contracts.IManterCategoriaBO may not be resolved. Check the configuration file and check it. type name.

I double-checked all my classes, and for me this is normal. Or is he missing anything?

I need help because I have a short time to take care of this ....

Regards, Marcelo.

+8
c # asp.net-mvc-4 unity-container n-tier-architecture
source share
6 answers

The problem is with your configuration file. You are mixing two concepts with some wrong syntax.

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

So your <register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" /> node is of type Biblioteca.Contracts.IManterCategoriaBO , which does not contain an assembly, so it cannot be found. Therefore, he must perform a search. You specified the <namespace ... /> nodes, so first try Biblioteca.Biblioteca.Contracts.IManterCategoriaBO, Biblioteca . Note the duplicate name Biblioteca.

Here is the adjusted 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="IManterCategoriaBO" mapTo="ManterCategoriaBO" /> <!-- Or this works --> <!--<register type="Biblioteca.Contracts.IManterCategoriaBO, Biblioteca" mapTo="Biblioteca.Business.ManterCategoriaBO, Biblioteca" />--> </container> </unity> </configuration> 
+11
source share

A little information that no one talks about anywhere is that I need to make a link to all the projects that will be used by unity.

So, in my solution, inside my Biblioteca web project, I had to turn to Biblioteca.Business and Biblioteca.Contracts in order to be able to go through the unity register without any error. I referred only to the latter.

This is unbelievable, but it was my problem !!! I thought unity could do some reflection using all the paths included in my unity.config file. But I was wrong. To make Unity work, you must specify all the projects that are inside the unity.config file. If I point to some namespace, I need to link the corresponding project.

I have already solved the problem, but I do not agree with this approach to make unity. But, anyway, now it works!

Thank you Tyler for your support.

+4
source share

As indicated in the previous comment, you can load reflective memory before calling UnityContainer.LoadConfiguration(...) . Here is a general way to do this.

Just make sure your assemblies can be found by name. This can be done by placing the assembly in the bin application directory. Or add the <probing> path to your application configuration. Or add them to the global assembly cache (GAC), but I do not recommend this.

 private static void LoadDependencyAssemblies() { UnityConfigurationSection section = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection; if (section == null) { throw new ConfigurationErrorsException("Unable to locate Unity configuration section."); } foreach (string assemblyName in section.Assemblies.Select(element => element.Name)) { try { Assembly.Load(assemblyName); } catch (Exception ex) { throw new ConfigurationErrorsException("Unable to load required assembly specified in Unity configuration section. Assembly: " + assembly, ex); } } } 
+2
source share

I also received this error message, but found a different set of reasons:

  • I am using the WiX installer, which did not correctly place some DLL files needed for the target assembly (in this example, these were Biblioteca.Business dependencies). So double check this.
  • I received an error message only when starting in release mode, debug mode worked fine. It turns out that the target mapTo class was not marked with the "public" attribute. For some reason, it didn't matter in debug mode, but it is a blocker in release mode.

Hope this helps someone since he blocked me for two days.

+1
source share

I just spent most of my time fixing this error.

In my case, there was no configuration error, but it turned out that one of the assemblies containing specific types depended on another assembly that it could not find.

Unfortunately, Unity did not hint at any problems with loading the assembly. In fact, the assembly appeared in the Modules window in Visual Studio Debugger. However, the type cannot be loaded.

0
source share

If you are here from Google, the easiest way to make it work is to delete the <alias> and <namespace> files and do the following.

 <container> <register type="MyApp.Repositories.IUserRepository, MyApp" mapTo="MyApp.Repositories.UserRepository, MyApp" /> </container> 

... where MyApp is your assembly name from the project property -> application.

0
source share

All Articles