Type name could not be resolved. Please check config file

Neeta picture Neeta · Feb 18, 2013 · Viewed 10.7k times · Source

Trying to unit test my application using moq and unity, and I'm getting errors where my service is an interface and cannot be resolved:

Error message:

    Test method Ecommerce_Test.Tests.LoginSuccessfulRedirectToActionLoad threw exception: 
System.InvalidOperationException: The type name or alias Ecommerce_Test.AuthenticationMock could not be resolved. Please check your configuration file and verify this type name.

Error Stack Trace:

    Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs: line 110
Microsoft.Practices.Unity.Configuration.RegisterElement.GetMappedType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs: line 128
Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs: line 0
Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs: line 114
Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Utility\EnumerableExtensions.cs: line 36
Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs: line 110
Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityConfigurationSection.cs: line 151
Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs: line 35
Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs: line 63
Ecommerce.Controllers.HomeController.Login(LoginModel model) in C:\Users\avmin!\Documents\Visual Studio 2010\Projects\Ecommerce_Mock\Ecommerce\Controllers\HomeController.cs: line 45
Ecommerce_Test.Tests.LoginSuccessfulRedirectToActionLoad() in C:\Users\avmin!\Documents\Visual Studio 2010\Projects\Ecommerce_Mock\Ecommerce_Test\Tests.cs: line 80

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="Ecommerce"/>
    <assembly  name="Ecommerce_Tests"/>

    <container>
      <register type="Ecommerce.Authentication.IAuthentication"
                mapTo="Ecommerce_Test.AuthenticationMock, Ecommerce_Test" />
    </container>
  </unity>
  <system.serviceModel>
      <bindings />
      <client />
  </system.serviceModel>
</configuration>

Usage:

public ActionResult Login(HomeModels.LoginModel model)
        {
            var unity = new UnityContainer().LoadConfiguration(); //error fired here!!!
            var proxy = unity.Resolve<IAuthentication>();


            if (ModelState.IsValid)
            {
                try
                {
                    Login log = new Login();
                    log.Username = model.Username;
                    log.Password = model.Password;
                    int result = proxy.Login(new Login { Username = log.Username, Password = log.Password });

                }
                catch (MemberAccessException e)
                {
                    ModelState.AddModelError("Login was not successful", e);
                }
            }
            return View("Login",model);
        }

Answer

Neeta picture Neeta · Feb 18, 2013

The solution turned out to be appending the name of the assembly to the end of the mapTo value. The config file is updated above to reflect this.

Assembly name found by right clicking on Ecommerce_Tests project > Properties.