Solved

error on resolving type when using Unity IoC

Posted on 2011-03-06
4
2,784 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now