Solved

error on resolving type when using Unity IoC

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

19 Experts available now in Live!

Get 1:1 Help Now