Solved

error on resolving type when using Unity IoC

Posted on 2011-03-06
4
2,990 Views
Last Modified: 2012-05-11
Hi,

I am getting an error:
The type name or alias ICategoryService could not be resolved. Please check your configuration file and verify this type name.

When trying to run this code from a website default.aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
     var catService = IoCWorker.Resolve<ICategoryrService>();
}

Open in new window


This is the web.config registration, and attached below are the classes involved.
Also I should mention that everything compiles great and I added references to the website for all the projects involved in this.
<unity>
		<typeAliases>
			<!-- Lifetime manager types -->
			<typeAlias alias="singleton" 
                       type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
			<typeAlias alias="perThread" 
                       type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity"/>
			<typeAlias alias="external" 
                       type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>

            <typeAlias alias="ICategoryService"
                       type="XComSolutions.FB.Application.Interfaces.Categories.ICategoryService, XComSolutions.FB.Application.Interfaces.Categories"/>
            <typeAlias alias="CategoryService"
                       type="XComSolutions.FB.Application.Services.Categories.CategoryService, XComSolutions.FB.Application.Services.Categories"/>
            
			<!-- User-defined type aliases -->
			<typeAlias alias="IRepositoryContext" 
                       type="XComSolutions.FB.Domain.Repository.IRepositoryContext, XComSolutions.FB.Domain.Repository"/>
			<typeAlias alias="RepositoryContext" 
                       type="XComSolutions.FB.Repository.EF.RepositoryContext, XComSolutions.FB.Repository.EF"/>
            
			<typeAlias alias="IGenericRepository" 
                       type="XComSolutions.FB.Domain.Repository.IGenericRepository`1, XComSolutions.FB.Domain.Repository"/>
			<typeAlias alias="GenericRepository" 
                       type="XComSolutions.FB.Repository.EF.GenericRepository`1, XComSolutions.FB.Repository.EF"/>
            
            <typeAlias alias="ICategoryRepository" 
                       type="XComSolutions.FB.Domain.Repository.Repositories.Categories.ICategoryRepository, XComSolutions.FB.Domain.Repository.Repositories.Categories"/>
			<typeAlias alias="CategoryRepository" 
                       type="XComSolutions.FB.Repository.EF.Repositories.Categories.CategoryRepository, XComSolutions.FB.Repository.EF.Repositories.Categories"/>
		</typeAliases>
		<containers>
			<container name="containerOne">
				<types>
					<type type="IRepositoryContext" mapTo="RepositoryContext">
						<lifetime type="singleton"/>
					</type>
					<type type="IGenericRepository" mapTo="GenericRepository">
						<lifetime type="singleton"/>
					</type>
					<!-- Categories -->
					<type type="ICategoryService" mapTo="CategoryService">
						<lifetime type="singleton"/>
					</type>
                    <type type="ICategoryRepository" mapTo="CategoryRepository">
                        <lifetime type="singleton"/>
                    </type>
				</types>
			</container>
		</containers>
	</unity>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XComSolutions.FB.Application.Interfaces.Categories;
using XComSolutions.FB.Domain.Model.Entities.Categories;
using XComSolutions.FB.Domain.Model;
using XComSolutions.FB.Domain.Repository.Repositories.Categories;

namespace XComSolutions.FB.Application.Services.Categories
{ 
    public class CategoryService : ICategoryrService
    {
        private ICategoryRepository _categoryRepository;

        public CategoryService(ICategoryRepository categoryRepository)
        {
            _categoryRepository = categoryRepository;
        }

        public List<Category> GetAll()
        {
            return _categoryRepository.GetAll();
        }


        public bool InsertCategory(string Name, int parent)
        {
            try
            {
                Category newCat = new Category();
                newCat.Description = "";
                newCat.Name = Name;
                newCat.ParentCategoryID = parent;
                newCat.Status = BaseStatus.Status.Active;
                newCat.LinkText = "";
                newCat.UpdatedBy = 100;
                newCat.CreatedBy = 100;
                newCat.UpdatedOn = DateTime.Now;
                newCat.CreatedOn = DateTime.Now;

                _categoryRepository.Add(newCat);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

Open in new window

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using System.Threading;
using System.Web;
using XComSolutions.FB.Domain.Model.Entities;
using XComSolutions.FB.Domain.Model;
using XComSolutions.FB.Domain.Repository;
using XComSolutions.FB.Infrastructure.IoC;

namespace XComSolutions.FB.Repository.EF
{
    public class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity
    {

        /// <summary>
        /// Returns the active object context
        /// </summary>
        private ObjectContext ObjectContext
        {
            get
            {
                return ObjectContextManager.GetObjectContext();
            }
        }

        public T Load(Expression<Func<T, bool>> whereCondition)
        {
            return default(T);
        }

        public IQueryable<T> GetQuery()
        {
            // TODO Check this works with inheritance
            return this.ObjectContext.CreateObjectSet<T>();
        }

        /// <summary>
        /// Provides explicit loading of object properties (EF v4.0 feature)
        /// </summary>
        public void LoadProperty(T entity, Expression<Func<T, object>> selector)
        {
            this.ObjectContext.LoadProperty(entity, selector);
        }

        public List<T> GetAll()
        {
            return GetQuery().ToList();
        }

        public List<T> GetAll(int index, int count)
        {
            return GetQuery().Skip(index).Take(count).ToList();
        }

        public List<T> FindBy(Func<T, bool> expression)
        {
            return GetQuery().Where(expression).ToList();
        }

        public List<T> FindBy(Func<T, bool> expression, int index, int count)
        {
            return GetQuery().Where(expression).Skip(index).Take(count).ToList();
        }

        public void Add(T entity)
        {
            // TODO - check if this works
            this.ObjectContext.AddObject(GetBaseType(typeof(T)).Name.ToString(), entity);
        }

        public void Delete(T entity)
        {
            this.ObjectContext.DeleteObject(entity);
        }

        private static Type GetBaseType(Type type)
        {
            Type baseType = type.BaseType;
            if (baseType != null && baseType != typeof(BaseEntity))
            {
                return GetBaseType(type.BaseType);
            }
            return type;
        }

        private static bool HasBaseType(Type type, out Type baseType)
        {
            Type originalType = type.GetType();
            baseType = GetBaseType(type);
            return baseType != originalType;
        }

    }
}

Open in new window

using System;
using XComSolutions.FB.Domain.Model.Entities.Categories;

namespace XComSolutions.FB.Domain.Repository.Repositories.Categories
{
    public interface ICategoryRepository : IGenericRepository<Category>
    {

    }
}

Open in new window

using System;
using System.Linq;
using System.Linq.Expressions;
using XComSolutions.FB.Domain.Model.Entities.Categories;
using XComSolutions.FB.Domain.Repository.Repositories.Categories;

namespace XComSolutions.FB.Repository.EF.Repositories.Categories
{
    public class CategoryRepository : GenericRepository<XComSolutions.FB.Domain.Model.Entities.Categories.Category>, ICategoryRepository
    {
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XComSolutions.FB.Domain.Model.Entities.Categories;
using XComSolutions.FB.Domain.Repository;

namespace XComSolutions.FB.Application.Interfaces.Categories
{
    public interface ICategoryrService
    {
        List<Category> GetAll();
        bool InsertCategory(string Name, int parent);
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using XComSolutions.FB.Domain.Model.Entities;
using XComSolutions.FB.Domain.Model;

namespace XComSolutions.FB.Domain.Repository
{
    public interface IGenericRepository<T> : IRepositoryBase where T : BaseEntity
    {

        /// <summary>
        /// Return strongly typed IQueryable
        /// </summary>
        IQueryable<T> GetQuery();

        /// <summary>
        /// Load entity from the repository (always query store)
        /// </summary>
        /// <typeparam name="T">the entity type to load</typeparam>
        /// <param name="where">where condition</param>
        /// <returns>the loaded entity</returns>
        T Load(Expression<Func<T, bool>> whereCondition);

        /// <summary>
        /// Provides explicit loading of object properties
        /// </summary>
        void LoadProperty(T entity, Expression<Func<T, object>> selector);

        /// <summary>
        /// Returns all entities for a given type
        /// </summary>
        /// <returns>All entities</returns>
        List<T> GetAll();

        /// <summary>
        /// Returns all entities for a given type
        /// </summary>
        /// <returns>All entities</returns>
        List<T> GetAll(int index, int count);

        /// <summary>
        /// Returns all entities for a given type which meet a certain boolean expression
        /// </summary>
        /// <returns>All entities</returns>
        List<T> FindBy(Func<T, bool> expression);

        /// <summary>
        /// Returns #count entities from #index entity 
        /// for a given type which meet a certain boolean expression
        /// </summary>
        /// <returns>All entities</returns>
        List<T> FindBy(Func<T, bool> expression, int index, int count); 

        /// <summary>
        /// Add entity to the repository
        /// </summary>
        /// <param name="entity">the entity to add</param>
        /// <returns>The added entity</returns>
        void Add(T entity);

        /// <summary>
        /// Mark entity to be deleted within the repository
        /// </summary>
        /// <param name="entity">The entity to delete</param>
        void Delete(T entity);

    }
}

Open in new window

0
Comment
Question by:sagir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 3

Author Comment

by:sagir
ID: 35046044
Please notice that I might have ICategoryrService
instead of
ICategoryService

Over there, but this is not the problem. I fixed that and still getting same error.

Thanks
0
 
LVL 3

Author Comment

by:sagir
ID: 35047674
No one with any answer on this one?
0
 
LVL 3

Accepted Solution

by:
sagir earned 0 total points
ID: 35048025
Ok I have solved the problem myself. The problem was that I did not specify the correct assembly as the second parameter of the type in the web.config when Aliasing my types.

I thought that the second param should be the path for the class but it should be the name of the referenced assembly.
0
 
LVL 3

Author Closing Comment

by:sagir
ID: 35407110
No other answer was given.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question