2010-07-03 39 views
0

我一直在下面史蒂芬·桑德森的書叫臨ASP.NET MVC框架,和我運行到一個例外:SportsStore:MVC編程

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; 

這裏是我的WindsorControllerFactory代碼:

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); 
     } 
    } 

我的Global.asax.cs代碼:

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

      RegisterRoutes(RouteTable.Routes); 

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

而且在web.config值:

<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> 

我的問題是爲什麼無法加載類型'做mainModel.Abstract.IProductsRepository'from assembly'DomainModel'。 ?

回答

1

對此錯誤的可能解釋是IProductsRepository接口不是public和/或未放置在DomainModel程序集的DomainModel.Abstract命名空間中。確保定義看起來是這樣的,它是DomainModel項目的一部分:對於您已經定義了DomainModel.Concrete.SqlProductsRepository, DomainModel

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

同樣的立場如此。