tags:

views:

60

answers:

2

I've been following Steven Sanderson's book called Pro ASP.NET MVC Framework, and I'm running into an exception:

Could not load type 'DomainModel.Abstract.IProductsRepository' from assembly 'DomainModel'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.TypeLoadException: Could not load type 'DomainModel.Abstract.IProductsRepository' from assembly 'DomainModel'

.

Line 19:         public WindsorControllerFactory()
Line 20:         {
Line 21:             container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
Line 22: 
Line 23:             var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t;

Here's my WindsorControllerFactory code:

public class WindsorControllerFactory : DefaultControllerFactory
    {
        WindsorContainer container;

        public WindsorControllerFactory()
        {
            container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

            var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t;

            foreach (Type t in controllerTypes)

                container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return (IController)container.Resolve(controllerType);
        }
    }

My Global.asax.cs code:

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
        }

And the web.config values:

<configSections>
    < section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>

<castle>
    <components>
      <component id="ProdsRepository" service="DomainModel.Abstract.IProductsRepository, DomainModel"
                  type="DomainModel.Concrete.SqlProductsRepository, DomainModel">
        <parameters>
          < connectionString>data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SportsStore.mdf;User Instance=true< /connectionString>
        </parameters>
      </component>
    </components>
  </castle>

My question is why could not load type 'DomainModel.Abstract.IProductsRepository' from assembly 'DomainModel'. ?

A: 

A possible explanation for this error is that the IProductsRepository interface is not public and/or is not placed inside the DomainModel.Abstract namespace in the DomainModel assembly. Make sure that the definition looks like this and that it is part of the DomainModel project:

namespace DomainModel.Abstract
{
    public interface IProductsRepository
    {
        ...
    }
}

The same stands true for the DomainModel.Concrete.SqlProductsRepository, DomainModel class that you've defined.

Darin Dimitrov
A: 

Thanks for your post. My code is below:

SqlProductsRepository:

namespace DomainModel.Concrete
{
    public class SqlProductsRepository : IProductsRepostory
    {
        private Table<Product> productsTable;

        public SqlProductsRepository(string connectionString)
        {
            productsTable = (new DataContext(connectionString)).GetTable<Product>();
        }

        public IQueryable<Product> Products
        {
            get
            {
                return productsTable;
            }
        }
    }
}

IProductsRepostory

namespace DomainModel.Abstract
{
    public interface IProductsRepostory
    {
        IQueryable<Product> Products
        {
            get;
        }
    }
}

Your description of the part I have written. But it still have exception.

Huang
You have a typo: it should be `IProductsRepository` and not `IProductsRepostory`. Also don't post answers. Modify your question in order to provide more details such as this.
Darin Dimitrov
Thanks your post.I edit public class SqlProductsRepository : IProductsRepository and public interface IProductsRepository, but it still not work.
Huang
Problem has been resolved.Thanks everyone.
Huang