Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

error on resolving type when using Unity IoC

Posted on 2011-03-06
4
Medium Priority
?
3,095 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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